java中file问题

来源:百度知道 编辑:UC知道 时间:2024/05/24 11:21:27
E:/a.txt文件中内容为:tom,19,man
类似信息共8行。要求输出内容为:
name:tom age:19 sex:man
等8行。

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class Test96 {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("e:\\a.txt");
BufferedReader sb = new BufferedReader(fr);
String tmp = sb.readLine();
while(tmp != null){//我这里是你有多少行我就读取多少行,如果你想只读8行就写个for循环,循环8次就可以了
String[] all = tmp.split(",");
System.out.println("name:"+all[0]+" age:"+all[1]+" sex:"+all[2]);
tmp = sb.readLine();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

import java.io.*;

public class ReadFile {
String str = null;
String name = null;
String age = null;
String sex = null;
public ReadFile() throws IOException{
File file = new File("a.tx