📄 maintainnamearchive.java
字号:
/*
* MaintainNameArchive.java E.L. 2001-08-15
*
*/
import java.io.*;
import javax.swing.JOptionPane;
class MaintainNameArchive {
public static void main(String[] args) throws Exception {
String filename = "nameFile.txt";
/*
* Reads all the names, and prints them on the screen.
*/
FileReader readConnToFile = new FileReader(filename);
BufferedReader reader = new BufferedReader(readConnToFile);
String result = "The register:\n";
String oneName = reader.readLine();
while(oneName != null) { // null means end-of-file
result += oneName + "\n";
oneName = reader.readLine();
}
JOptionPane.showMessageDialog(null, result);
reader.close();
/*
* Does the user want more names to be registered?
*/
int answer = JOptionPane.showConfirmDialog(null, "Do you want to add more names? ",
"Archive", JOptionPane.YES_NO_OPTION);
if (answer == JOptionPane.YES_OPTION) {
FileWriter writeConnToFile = new FileWriter(filename, true);
PrintWriter printer = new PrintWriter(new BufferedWriter(writeConnToFile));
while (answer == JOptionPane.YES_OPTION) {
String newName = JOptionPane.showInputDialog("Name: ");
printer.println(newName);
answer = JOptionPane.showConfirmDialog(null, "Do you want to add more names? ",
"Archive", JOptionPane.YES_NO_OPTION);
}
printer.close();
}
System.exit(0);
}
}
/* Example Run:
=> The data file before program run:
Tony Kingsley
Ken Brown
Margaret Gibson
=> The user enters two new names:
Matthew Johnson
Peter Adams
=> The data file after program run:
Tony Kingsley
Ken Brown
Margaret Gibson
Matthew Johnson
Peter Adams
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -