我们如何在 java 中的每行末尾连接一个字符串?

How can we concat a String at the end of each line in java?

我有一些必须附加到每行末尾的唯一 ID。我不想创建另一个文件,我想在用于逐行读取并基于该行生成唯一 ID 的同一个文件中执行此操作。
我使用 java 进行此操作,这是我的代码,

String filename="location.txt";
    Path pathToFile = Paths.get(filename);
    FileWriter fstream = null;
    BufferedWriter bw=null;
    PrintWriter pw=null;
    try {
        fstream = new FileWriter(filename, true);
        bw=new BufferedWriter(fstream);
        pw=new PrintWriter(bw);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try (BufferedReader br = Files.newBufferedReader(pathToFile, StandardCharsets.US_ASCII)) { 
        // read the first line from the text file 
        String line = br.readLine(); 
        // loop until all lines are read 
        while (line!=null) { 
            // use string.split to load a string array with the values from 
            // each line of 
            // the file, using a " " as the delimiter
            String[] attributes = line.split(" "); 

            System.out.print("Sensor["+attributes[0]+"] : ");
            double x=Double.parseDouble(attributes[1]);
            double y=Double.parseDouble(attributes[2]);
            GeoHash geoCode=GeoHash.withCharacterPrecision(x, y, 10);
            System.out.print(geoCode+"\n");
            pw.println(line+" "+geoCode);

            line = br.readLine(); 
        } 
        br.close();
        pw.close();
        bw.close();
        fstream.close();
    } catch (IOException ioe) { 
            ioe.printStackTrace(); 
    }
    System.out.println("Generated!!");
} 

目前通过 运行 这段代码,我将在我的文件末尾获得附加行作为新行,如下所示: 输出:

1 21.5 23
2 24.5 20
3 19.5 19
4 22.5 15
5 24.5 121 21.5 23 seb673undf
2 24.5 20 skq5rh5dcg
3 19.5 19 s7mwbmg7sp
4 22.5 15 sk48j248j2
5 24.5 12 sk2e3h44u7

有什么办法可以解决吗?或者任何其他简单的方法来做到这一点?

不是将每一行写入文件,而是在完成文件读取后将数据附加到每个 while 循环的字符串生成器,您只需清除文件中的数据,然后将字符串生成器数据写入该文件即可。

    package application;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.text.ParseException;

    public class T {
        public static void main(String[] args) throws ParseException {
            String filename="location.txt";
            Path pathToFile = Paths.get(filename);
            FileWriter fstream = null;
            BufferedWriter bw=null;
            PrintWriter pw=null;
            try {
                fstream = new FileWriter(filename, true);
                bw=new BufferedWriter(fstream);
                pw=new PrintWriter(bw);
            } catch (IOException e) {
                e.printStackTrace();
            }

            StringBuilder sb = new StringBuilder();
            try (BufferedReader br = Files.newBufferedReader(pathToFile, StandardCharsets.US_ASCII)) { 
                String line = br.readLine(); 
                while (line!=null) { 
                    String[] attributes = line.split(" "); 
                    System.out.print("Sensor["+attributes[0]+"] : ");
                    double x=Double.parseDouble(attributes[1]);
                    double y=Double.parseDouble(attributes[2]);
                    GeoHash geoCode=GeoHash.withCharacterPrecision(x, y, 10);
                    System.out.print(geoCode+"\n");
                    pw.println(line+" "+geoCode);
                    sb.append(line+" "+geoCode);
                    line = br.readLine(); 
                } 
                PrintWriter writer = new PrintWriter(filename);
                writer.print("");
                writer.close();
                br.close();
                pw.close();
                bw.close();
                fstream.close();
            } catch (IOException ioe) { 
                    ioe.printStackTrace(); 
            }
            System.out.println("Generated!!");
        } 

    }