gpt4 book ai didi

java - 在没有任何数据格式的特定行中写入文件

转载 作者:行者123 更新时间:2023-11-30 08:40:42 25 4
gpt4 key购买 nike

我想做的是当用户想要编辑他的个人资料然后按下保存更改时我想从所有 JTextFields 中获取文本并在该特定行写入文件。然而,经过 3 天的尝试没有运气。我已经查看了这里所有关于读取和写入文件的帖子,但没有任何运气,好吧,所有方法都在下面,我将解释它们

public void save() throws IOException{
String changes = name.getText() + "" + dob.getText() + ""
+ "" + address.getText() + "" + town.getText()
+ "" + number.getText();
String whichLine = this.staff_no.getText();
this.name.setEditable(false);
this.dob.setEditable(false);
this.address.setEditable(false);
this.town.setEditable(false);
this.number.setEditable(false);
users.saveChanges(changes,whichLine);
}

我在这里创建一个字符串,其中包含来自 JTextFields 的所有文本,将它们设置回 false 并调用 saveChanges 方法(假设“替换”这个新字符串的方法)

public int getIndex(String staffno){
for(User user : allUsers){
if(user.getStaff_No().contains(staffno)){
this.index = allUsers.indexOf(staffno);
break;
}
}
return this.index;
}

public void saveChanges(String changes, String whichLine) throws IOException{
try {
int count = 0;
FileWriter writer = new FileWriter("records.txt", true);
bw = new BufferedWriter(writer);
while(scan.hasNext()){
count++;
if(count == getIndex(whichLine)){
bw.write(changes);
bw.newLine();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally{
if(bw != null){
bw.close();
reader.close();
}
}
}

现在我正在获取用户的 Staffno 并通过我的用户 ArrayLists 运行它以找到它的索引让我们说它是 0。然后我循环通过我之前打开的扫描仪并试图找到我可以找到哪一行覆盖该行。但是我遇到了异常错误,例如 Stream closed 或 NoSuchAnElementException 或根本没有错误但没有写我的行或保存它。

这是我打开文件的方法

public void open(){
try {
URL path = Users.class.getResource("records.txt");
File f = new File(path.getFile());
reader = new BufferedReader(new FileReader(f));
scan = new Scanner(reader);
} catch (FileNotFoundException e) {
System.out.println("File not found");
}
}

我不关闭扫描仪的原因是我会继续用它做其他事情

我意识到这是一个特定的场景,而不是一个简单的读/写场景,因此非常感谢您的帮助。

最佳答案

您正在尝试做的事情 - 覆盖文件中间的数据 - 只能在您写入与要替换的字节数完全相同的字节数时才能完成。也就是说,您不能用不同的行覆盖文件中的一行,除非它们的字节数完全相同(不一定是相同的字符数,具体取决于字符编码)。

您还让扫描器保持打开状态,以查找您正在积极写入的文件,该文件只是询问与缓冲相关的问题。

解决问题的绝对最简单的方法是按照 coolguy 的建议将所有行读入内存,根据需要替换或添加行,然后将其全部写回。

但是,如果您不能或不想将整个文件缓存在内存中,您可以逐行读取文件,然后将它们写回 - 在您更改相关行之后 - 到不同的文件,最后用新文件替换旧文件。

public static void writeChanges(String changes, int whichLine) throws IOException {
// The files we're working with
final File database = new File("database.txt");
final File newDatabase = new File("database.new.txt");
try(BufferedReader reader = new Bufferedreader(
new InputStreamReader(new FileInputStream(database)));
PrintWriter writer = new PrintWriter(newDatabase)) {
int line = 0;
String line;
// Keep reading lines from the source file until we've read them all
while((line = reader.readLine()) != null) {
if(line == whichLine) {
// Write the modified line
writer.println(changes);
} else {
// Write the original line
writer.println(line);
}
line++;
}
}
// Replace the old database with the new
Files.move(newDatabase.toPath(), database.toPath(), StandardCopyOption.REPLACE_EXISTING);
}

关于java - 在没有任何数据格式的特定行中写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35525203/

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