按行读取文件studentInfo.txt的内容,获取学生姓名并写入到studentName.txt文件中,且给每一行按序加上行号;获取studentName.txt文件的长度及修改时间。
studentInfo.txt文件内容格式如下:
王珊珊,王珊珊,20132213806,2015-10-10 14:30:56
张国辉,张国辉,20132213944,2015-10-10 14:31:05
蒋宇宙,蒋宇宙,20132213906,2015-10-10 14:31:24
王燕萍,王燕萍,20132213847,2015-10-10 14:31:16
public class FlleIo {
public static void main(String[] args){
try{
File file = new File("studentName.txt");
if(!file.exists()){
file.createNewFile();//创建新文件
}
BufferedWriter bw = new BufferedWriter(new FileWriter(file));//写入
BufferedReader br = new BufferedReader(new FileReader("studentInfo.txt"));//读取
String line;
int number = 1;
while((line = br.readLine())!= null){
String[] sc = line.split(",");//逗号分割
bw.write(number+sc[0].trim());
bw.newLine();
bw.flush();
number++;
}
bw.close();
br.close();
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyy-MM-dd HH:mm:ss.SSS");
String dateTime = df.format(new Date(file.lastModified()));
System.out.println("文件修改时间:"+dateTime);
System.out.println("文件长度:"+file.length());
}
catch (IOException e){
e.printStackTrace();
}
}
}
运行结果:
新文件内容: