原题为
编写java程序,往一个txt文件里写入学生的基本信息,然后再读出这些信息并打印出来,最后把该文件拷贝到指定位置并在文件名前加入日期信息进行备份。
中间有些不成熟的地方还请见谅
代码如下
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文件里写入学生的基本信息
*
* @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("有内容 将追加");
buWriter.newLine();// 换行符
System.out.println("输入学生姓名");
buWriter.append(s.next() + " ");
System.out.println("输入性别");
buWriter.append(s.next() + " ");
System.out.println("输入学号");
buWriter.append(s.next());
System.out.println("输入完成");
} else {
System.out.println("无内容 将加入内容");
buWriter.append("姓名 性别 学号");
buWriter.newLine();// 换行符
System.out.println("输入学生姓名");
buWriter.append(s.next() + " ");
System.out.println("输入性别");
buWriter.append(s.next() + " ");
System.out.println("输入学号");
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); // 输出当前系统时间
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();
}
}