给出以下题目的Java代码:按行读取文件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
时间: 2024-02-03 12:15:14 浏览: 42
以下为题目对应的Java代码:
```
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
public class Main {
public static void main(String[] args) {
try {
// 读取studentInfo.txt文件内容
File file = new File("studentInfo.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
int lineNumber = 1;
FileWriter writer = new FileWriter("studentName.txt");
while ((line = reader.readLine()) != null) {
// 获取学生姓名并写入到studentName.txt文件中
String name = line.split(",")[0];
writer.write(lineNumber + ". " + name + "\n");
lineNumber++;
}
reader.close();
writer.close();
// 获取studentName.txt文件的长度及修改时间
File studentNameFile = new File("studentName.txt");
long length = studentNameFile.length();
Date lastModified = new Date(studentNameFile.lastModified());
System.out.println("Length: " + length);
System.out.println("Last modified: " + lastModified);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```