(1)按行读取文件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,用java编写
时间: 2024-02-03 09:14:53 浏览: 29
以下是Java代码实现:
```java
import java.io.*;
public class StudentInfoReader {
public static void main(String[] args) {
try {
// 读取文件
BufferedReader reader = new BufferedReader(new FileReader("studentInfo.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("studentName.txt"));
String line;
int lineNumber = 1;
while ((line = reader.readLine()) != null) {
// 获取学生姓名
String[] info = line.split(",");
String name = info[0];
// 写入到文件中
writer.write(lineNumber + ". " + name + "\n");
lineNumber++;
}
reader.close();
writer.close();
// 获取文件信息
File file = new File("studentName.txt");
System.out.println("文件长度:" + file.length());
System.out.println("文件修改时间:" + new java.util.Date(file.lastModified()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
该程序使用BufferedReader按行读取studentInfo.txt文件的内容,获取学生姓名并写入到studentName.txt文件中,并给每一行按序加上行号。然后使用File类获取studentName.txt文件的长度及修改时间。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)