Read file by line and write by line with the line number added

时间:2015-05-12 22:12:45

标签: java file line

The solution works and is capsulated and everything is fine, but .... I wonder if there is a better solution, maybe with new functions from Java 8 or some other improvements that can be done?

Main.java

%20

ReadFile.java

package test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

class Main {
public static void main(String[] args) {

    // zum speichern der Zeilen je Arrayfeld
    List<String> lines = new ArrayList<String>();

    // Inputdatei bestimmen
    File file = new File("C:\\...\\test.txt");

    // Inputdatei zeilenweise einlesen
    ReadFile readfile = new ReadFile();
    try {
        // und in der ArrayList "lines" speichern
        lines = readfile.byLine(file);
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Inputdatei zeilenweise schreiben (neue Datei) mit Zeilennummer
    WriteFile writefile = new WriteFile();
    try {
        writefile.byLine(lines);
    } catch (IOException e) {
        e.printStackTrace();
    }

}   
}

WriteFile.java

package test;

import java.util.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class ReadFile {

List<String> lines = new ArrayList<String>();
String line;
int lineNumber = 0;

protected List<String> byLine(File file) throws IOException {
    // Inputdatei einlesen
    FileReader fileReader = new FileReader(file);
    BufferedReader bufferedReader = new BufferedReader(fileReader);

    // schauen ob Zeilenende erreicht wurde
    while ((line = bufferedReader.readLine()) != null ) {
        // Zeilen in Array speichern
        lines.add(line);
    }

    // Reader schließen
    bufferedReader.close();
    // Array zurückgeben
    return lines;
}
}

2 个答案:

答案 0 :(得分:1)

Maybe the class will suite you (pay attention to int widthDp = (int)(img.getWidth() / density); method): https://docs.oracle.com/javase/7/docs/api/java/io/LineNumberReader.html

答案 1 :(得分:0)

Update

Didn't think file size was a concern given your posted example, but if it is you can read your file and write to a temp file all at once and then delete your old file and rename the new file to the old file name.

$pass = "dongs";
for($i = 0; $i < 5; $i++) $pass = md5($pass); //$pass will be hashed with md5() 5 times

If you're not dead set on having a reading & writing class, you could shorten all of your code to this...

public static void main(String[] args) throws Exception {
    insertLineNumbersIntoFile("C:\\...\\test2.txt");
}

private static void insertLineNumbersIntoFile(String filePath) throws Exception {
    String tempFile = "temp.txt";

    // Open reader and writer
    FileWriter writer = new FileWriter(tempFile);
    FileReader fileReader = new FileReader(filePath);
    BufferedReader bufferedReader = new BufferedReader(fileReader);

    // Read lines
    int lineNumber = 1;
    String line = "";
    while ((line = bufferedReader.readLine()) != null) {
        // Insert line number
        line = "*/ " + lineNumber + " /* " + line;
        // Write new line to new file
        writer.write(line + "\r\n");
        // Increment line number
        lineNumber++;
    }

    // Close reader and writer
    bufferedReader.close();
    writer.close();

    // Delete old file and rename new file to old
    File oldFile = new File(filePath);
    File newFile = new File(tempFile);

    oldFile.delete();
    newFile.renameTo(oldFile);
}