My code is when user type in the Name text filed, it will export as text file and record the Name. For example,
I typed 1st time " James " in the Name text filed, it will appear " James " in the text file. I type second time " Agnes " , it appears " Agnes " in the text file. But It only appear one name in the text file.
I want all the names appear whatever user type in the text filed. How do i modify my codes?
try { new JTextField(); // create new file String path="C:\\\\export.txt"; File file = new File(path); // if file doesnt exists, then create it if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); // write in file // bw.write(txtName.getText()); if (txtName.getText() ==null) { bw.write(txtName.getText()); bw.write('\\n'); System.out.print("1st Name:" +txtName.getText()); } else if (txtName.getText()!=null) { // bw.write('\\n'); bw.write(txtName.getText()); System.out.print("2nd Name:" +txtName.getText()); } // close connection bw.flush(); bw.close(); fw.close(); } catch(Exception e) { System.out.println(e); }
Please advise.Thanks
写入文件时应使用追加模式,例如:
FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
Edit again:
FileWriter fileWritter = new FileWriter(file.getName(),true);
BufferedWriter bw = new BufferedWriter(fileWritter);
if (txtName.getText()!=null){
//this line edited
bw.write(txtName.getText()+"\n");
System.out.print("2nd Name:" +txtName.getText());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.