txt读取文本

时间:2014-10-09 04:40:48
【文件属性】:

文件名称: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 alist = new ArrayList(); int bb=0; for (int i = 0; i < list.size(); i++) { String s= (String )list.get(i); System.out.println("s的值是:"+s);//循环读取每一行的值 String[] sArray =s.split(",");//用逗号分割每一行的属性 //给实体属性循环设值 n.setName(sArray[0]); n.setAge(sArray[2]); n.setMany(Integer.parseInt(sArray[3])); n.setSex(sArray[1]); alist.add(n); Names b =(Names)alist.get(i); System.out.println("------------------"+b.getAge()); bb=bb+b.getMany(); } System.out.println(bb); } }


网友评论