gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:47:03 24 4
gpt4 key购买 nike

我有一些唯一的 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!!");
}

}

关于java - java中如何在每一行的末尾连接一个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48127739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com