gpt4 book ai didi

java - 如何通过jtextfield替换文件中的一行?

转载 作者:行者123 更新时间:2023-12-02 10:54:58 24 4
gpt4 key购买 nike

这个程序允许用户搜索某个项目,然后该程序将这些项目拆分并将它们存储在 4 个不同的文本字段中。但是,下面是将检索到的项目更新回文件的方法。问题是每当用户单击更新按钮时,该行确实更新成功,但文件中的其他行被删除!

我使用arraylist来存储搜索到的项目,并使用临时文件来写入该项目,然后重命名它。

file.txt

ramal hotel1 10 uk

bren hotel2 20 france

gil hotel3 30 china
<小时/>
//Update to file
button9.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){

try {

String stringSearch = textfield1.getText();

File filetemp = File.createTempFile("TempFileName", ".tmp", new File("/"));
BufferedWriter writer = new BufferedWriter(new FileWriter(filetemp));
File file = new File("file.txt");
BufferedReader bf = new BufferedReader(new FileReader(file));


int linecount = 0;
String line;

ArrayList<String> list = new ArrayList<String>();
while (( line = bf.readLine()) != null){
list.add(line);
linecount++;


int indexfound = line.indexOf(stringSearch);

if (indexfound > -1) {

String a = textfield1.getText();
String b = textfield2.getText();
String c = textfield3.getText();
String d = textfield4.getText();




String[] word = line.split("\t");
String firstword = word[0];
String secondword = word[1];
String thirdword = word[2];
String fourthword = word[3];




writer.write(line.replace("\t", "\t").replace(firstword, b).replace(secondword, c).replace(thirdword, d));
writer.flush();

}

writer.newLine();
}
writer.close();
bf.close();
filetemp.renameTo(file);
filetemp.deleteOnExit();


FileChannel src = new FileInputStream(filetemp).getChannel();
FileChannel dest = new FileOutputStream(file).getChannel();
dest.transferFrom(src, 0, src.size());



}catch (IOException e) {

System.out.println("IO Error Occurred: " + e.toString());

}

}
});

最佳答案

您仅将该数据保存回包含您正在搜索的字符串的文件,而忽略其他行。

将其添加到代码中 if block 之后

else{
writer.write(line);
writer.flush();
}

就在以下行之前:

writer.newLine();

所以你的代码将如下所示:

//Update to file
button9.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){

try {

String stringSearch = textfield1.getText();

File filetemp = File.createTempFile("TempFileName", ".tmp", new File("/"));
BufferedWriter writer = new BufferedWriter(new FileWriter(filetemp));
File file = new File("file.txt");
BufferedReader bf = new BufferedReader(new FileReader(file));


int linecount = 0;
String line;

ArrayList<String> list = new ArrayList<String>();
while (( line = bf.readLine()) != null){
list.add(line);
linecount++;


int indexfound = line.indexOf(stringSearch);

if (indexfound > -1) {

String a = textfield1.getText();
String b = textfield2.getText();
String c = textfield3.getText();
String d = textfield4.getText();




String[] word = line.split("\t");
String firstword = word[0];
String secondword = word[1];
String thirdword = word[2];
String fourthword = word[3];




writer.write(line.replace("\t", "\t").replace(firstword, b).replace(secondword, c).replace(thirdword, d));
writer.flush();

}
else{
writer.write(line);
writer.flush();
}
writer.newLine();
}
writer.close();
bf.close();
filetemp.renameTo(file);
filetemp.deleteOnExit();


FileChannel src = new FileInputStream(filetemp).getChannel();
FileChannel dest = new FileOutputStream(file).getChannel();
dest.transferFrom(src, 0, src.size());



}catch (IOException e) {

System.out.println("IO Error Occurred: " + e.toString());

}

}
});

关于java - 如何通过jtextfield替换文件中的一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14564500/

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