如何检查文本板中的下一个空行并附加下一个输入
How to check the next empty line in textpad and append the next input
您好,我的 UI 有问题。目前,我希望我的 UI 在每次单击保存时将输入附加到文本板中。单击保存按钮后,它将 return 到一个菜单,询问用户是否要输入另一个输入。如果用户单击是,它将 return 到输入部分。但是,当我第二次尝试输入时,它会覆盖我最初写的内容。如何更改我的代码以解决此问题?
private void saveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
BufferedWriter output = null;
FileInputStream fs = null;
try {
// TODO add your handling code here:
File myFile = new File("C:/Users/kai/Desktop/sample.txt");
fs = new FileInputStream("C:/Users/kai/Desktop/sample.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
myFile.createNewFile();
output = new BufferedWriter(new FileWriter(myFile));
//output.flush();
for(int i = 0; i<100; ++i){
String line = br.readLine();
if(line.equals(null)){
String name = text1.getText();
String id = text2.getText();
output.write(name + " " + id);
break;
}
}
// output.newLine();
} catch (IOException ex) {
Logger.getLogger(setup.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
output.close();
} catch (IOException ex) {
Logger.getLogger(setup.class.getName()).log(Level.SEVERE, null, ex);
}
}
this.dispose();
}
However when I try inputing the second time, it will overwrite what I have written initially.
以附加模式打开 FileWriter
。
阅读 FileWriter
API 以找到合适的构造函数来使用。
一个新的 sample.txt 正在创建,甚至在从这里读取旧的之前。一种方法是将文本输入读入字符串并在新输出中添加换行符 ("\n") 并与输入连接以创建新的相同文件。
特别感谢 camickr 和 Kesavacharan。以下是我编辑的版本。如果您发现我的代码可以改进,请随时发表评论。
private void saveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
BufferedWriter output = null;
FileInputStream fs = null;
FileWriter fout = null;
try {
// TODO add your handling code here:
File myFile = new File("C:/Users/kai/Desktop/sample.txt");
fs = new FileInputStream("C:/Users/kai/Desktop/sample.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
output = new BufferedWriter(new FileWriter(myFile,true));
PrintWriter fileout = new PrintWriter(output,true);
for(int i = 0; i<100; ++i){
String line = br.readLine();
if(line==null){
String name = text1.getText();
String id = text2.getText();
fileout.println(name + " " + id);
break;
}
}
} catch (IOException ex) {
Logger.getLogger(setup.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
output.close();
} catch (IOException ex) {
Logger.getLogger(setup.class.getName()).log(Level.SEVERE, null, ex);
}
}
this.dispose();
}
您好,我的 UI 有问题。目前,我希望我的 UI 在每次单击保存时将输入附加到文本板中。单击保存按钮后,它将 return 到一个菜单,询问用户是否要输入另一个输入。如果用户单击是,它将 return 到输入部分。但是,当我第二次尝试输入时,它会覆盖我最初写的内容。如何更改我的代码以解决此问题?
private void saveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
BufferedWriter output = null;
FileInputStream fs = null;
try {
// TODO add your handling code here:
File myFile = new File("C:/Users/kai/Desktop/sample.txt");
fs = new FileInputStream("C:/Users/kai/Desktop/sample.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
myFile.createNewFile();
output = new BufferedWriter(new FileWriter(myFile));
//output.flush();
for(int i = 0; i<100; ++i){
String line = br.readLine();
if(line.equals(null)){
String name = text1.getText();
String id = text2.getText();
output.write(name + " " + id);
break;
}
}
// output.newLine();
} catch (IOException ex) {
Logger.getLogger(setup.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
output.close();
} catch (IOException ex) {
Logger.getLogger(setup.class.getName()).log(Level.SEVERE, null, ex);
}
}
this.dispose();
}
However when I try inputing the second time, it will overwrite what I have written initially.
以附加模式打开 FileWriter
。
阅读 FileWriter
API 以找到合适的构造函数来使用。
一个新的 sample.txt 正在创建,甚至在从这里读取旧的之前。一种方法是将文本输入读入字符串并在新输出中添加换行符 ("\n") 并与输入连接以创建新的相同文件。
特别感谢 camickr 和 Kesavacharan。以下是我编辑的版本。如果您发现我的代码可以改进,请随时发表评论。
private void saveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
BufferedWriter output = null;
FileInputStream fs = null;
FileWriter fout = null;
try {
// TODO add your handling code here:
File myFile = new File("C:/Users/kai/Desktop/sample.txt");
fs = new FileInputStream("C:/Users/kai/Desktop/sample.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
output = new BufferedWriter(new FileWriter(myFile,true));
PrintWriter fileout = new PrintWriter(output,true);
for(int i = 0; i<100; ++i){
String line = br.readLine();
if(line==null){
String name = text1.getText();
String id = text2.getText();
fileout.println(name + " " + id);
break;
}
}
} catch (IOException ex) {
Logger.getLogger(setup.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
output.close();
} catch (IOException ex) {
Logger.getLogger(setup.class.getName()).log(Level.SEVERE, null, ex);
}
}
this.dispose();
}