用一个文件来保存学生的信息如下 :
学号
姓名
年龄
性别
学校等;写一个程序,可以对该数据文件进行读写操作,完成以下功能;
1.可以查找、增加、删除、修改学生信息。2.文件格式可以用文本或二进制。3.提供基本的交互功能;4.写一个配置文件,要求程序启动时获取如下信息。
一,提示信息的语言,是中文还是英文。
二,学生数据文件应该存放的文件名。
要求:
尽量将对配置文件的操作封闭成类,
学号
姓名
年龄
性别
学校等;写一个程序,可以对该数据文件进行读写操作,完成以下功能;
1.可以查找、增加、删除、修改学生信息。2.文件格式可以用文本或二进制。3.提供基本的交互功能;4.写一个配置文件,要求程序启动时获取如下信息。
一,提示信息的语言,是中文还是英文。
二,学生数据文件应该存放的文件名。
要求:
尽量将对配置文件的操作封闭成类,
再用集合类保存,
写文件的时候用ObjectOutputStream.
读的时候用ObjectInputStream.
增删改查可在集合类上进行,最后进行回写文件即可
这个方法实现的是在jTextArea内输入数据后,保存在txt文件中.private void Savefile() {
String text = null;
PrintWriter pf = null;
try {
text = jTextArea1.getText();
if (!(text == null || text.equals(""))) {
if (item == null || item.equals("")) {
Object title = JOptionPane.showInputDialog("請輸入標題");
// title =jList1.getSelectedValue();
pf = new PrintWriter(
new FileWriter(myPath + title + ".txt"), true);
} else {
pf = new PrintWriter(new FileWriter(myPath + item), true);
}
pf.println(text);
pf.close();
// n++;
showFileList();
JOptionPane.showMessageDialog(this, "保存成功", "信息對話框",
JOptionPane.INFORMATION_MESSAGE);
item = null; } else {
JOptionPane.showMessageDialog(this, "奶奶的,輸入點東西再存!", "信息對話框",
JOptionPane.INFORMATION_MESSAGE);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "保存失敗", "信息對話框",
JOptionPane.INFORMATION_MESSAGE);
}
}这个方法是读取已经保存了的txt文件.private void Openfile() {
item = jList1.getSelectedValue();
if (!(item == null || item.equals(""))) {
BufferedReader br;
String myString = null;
String resultString = new String();
try {
br = new BufferedReader(new FileReader(myPath + item));
while ((myString = br.readLine()) != null) {
resultString = resultString + myString + "\n";
}
br.close();
jTextArea1.setText(resultString);
} catch (FileNotFoundException e) {
// TODO 自?生成 catch ?
e.printStackTrace();
} catch (IOException e) {
// TODO 自?生成 catch ?
e.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(this, "請選擇一個文件", "信息對話框",
JOptionPane.INFORMATION_MESSAGE);
}
}
xml doc manipulation:
Element root = new Element(<root element tag>);
Document doc = new Document(root);root.addContent(new Element("學號").setText(<學號>));
so on...write file:
BufferedOutputStream bos = new BufferedOutputStream(new FileInputStream(<file path>));
XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
xmlOut.output(doc, bos);
bos.close();read file:
Document doc = new SAXBuilder().build(<file path>);大概就這樣了