文件名称:txt读取文本
文件大小:2KB
文件格式:TXT
更新时间:2014-10-09 04:40:48
文本的读写
package xmlTest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class FileTxt {
public static void main(String[] args) {
String path = "D:\\Project\\test\\people.txt";
Names n =new Names();//装载属性的实体类
List list = new ArrayList();//用来装载一行行读取后的数据集合
InputStream inputStream=null;//写入流
BufferedReader bufReader =null;//行读取器
try{
FileWriter fw = new FileWriter(path);
for (int i = 0; i < 10; i++) {
fw.write("小王,男," + (i + 1) + "岁," + (i * 10 + 500) + "\n");
}
fw.close();
inputStream = new FileInputStream(path);
bufReader = new BufferedReader(new InputStreamReader(inputStream,"GBK"));
for(String line = bufReader.readLine(); line!=null; line=bufReader.readLine()){
System.out.println(line);
list.add(line);
}
}catch (Exception e) {
// TODO: handle exception
}finally{
try {
inputStream.close();
bufReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
List