原題為
編寫java程序筷凤,往一個txt文件里寫入學(xué)生的基本信息,然后再讀出這些信息并打印出來,最后把該文件拷貝到指定位置并在文件名前加入日期信息進(jìn)行備份绘闷。
中間有些不成熟的地方還請見諒
代碼如下
package zz.zd.ty;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
/**
* 往一個txt文件里寫入學(xué)生的基本信息
*
* @see 然后再讀出這些信息并打印出來
* @author sk
*
*/
public class Input_Info {
// made file
static void InputInfo() throws IOException {
File file = new File("E:\\Sutdent");
String stxt = "Student_Info.txt";
// judge is or no exists
if (!file.exists()) {
file.mkdir();
File file2 = new File(file, stxt);
file2.createNewFile();
}
BufferedReader buffReader = new BufferedReader(new InputStreamReader(
new FileInputStream(file + "\\" + stxt), "utf-8"));
BufferedWriter buWriter = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(new File(file + "\\Student_Info.txt"),
true), "utf-8"));
String temp = null;
// plus content
Scanner s = new Scanner(System.in);
if ((temp = buffReader.readLine()) != null) {
System.out.println("有內(nèi)容 將追加");
buWriter.newLine();// 換行符
System.out.println("輸入學(xué)生姓名");
buWriter.append(s.next() + " ");
System.out.println("輸入性別");
buWriter.append(s.next() + " ");
System.out.println("輸入學(xué)號");
buWriter.append(s.next());
System.out.println("輸入完成");
} else {
System.out.println("無內(nèi)容 將加入內(nèi)容");
buWriter.append("姓名 性別 學(xué)號");
buWriter.newLine();// 換行符
System.out.println("輸入學(xué)生姓名");
buWriter.append(s.next() + " ");
System.out.println("輸入性別");
buWriter.append(s.next() + " ");
System.out.println("輸入學(xué)號");
buWriter.append(s.next());
System.out.println("輸入完成");
}
buffReader.close();
buWriter.close();
// print to screen
BufferedReader buffReader2 = new BufferedReader(new InputStreamReader(
new FileInputStream(file + "\\" + stxt), "utf-8"));
Date now = new Date();
SimpleDateFormat datformat = new SimpleDateFormat(
"yyyy-MM-dd HH_mm_ss ");
String now2 = datformat.format(now); // 輸出當(dāng)前系統(tǒng)時間
String txt = now2 + " Student2_Info.txt";
BufferedWriter buWriter2 = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(new File(file + "\\" + txt)), "utf-8"));
String temporary;
String temp2 = null;
while ((temporary = buffReader2.readLine()) != null) {
System.out.println(temporary);
temp2 = temporary;
buWriter2.append(temp2 + "\r\n");
}
buffReader2.close();
buWriter2.close();
}
}