欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

文件解析,,package my.t

来源: javaer 分享于  点击 46466 次 点评:110

文件解析,,package my.t


package my.test;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.util.ArrayList;import java.util.List;public class FileTest {    private final String filePath = "D://a";    public void testMethod() {        People people = new People(1, "Number:" + 1, "pwd:" + 1);        addRecord(people);        getRecords();    }    public static void main(String[] args) {        FileTest test = new FileTest();        test.testMethod();    }    /**向文件中一行行追加记录*/    private void addRecord(People people) {        File file = getFile();        BufferedWriter bw = null;        BufferedReader br = null;         String content = Record2Obj(people);        try {            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));            br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));            if(br.readLine() != null) { //这里判断第一次向文件中写数据时不换行                bw.newLine();            }            bw.append(content);        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch(IOException e) {            e.printStackTrace();        } finally {            try {                if(bw != null)                    bw.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }    /**从文件中按行读取每一条记录*/    private People[] getRecords() {        File file = getFile();        List<People> arrs = new ArrayList<People>();        FileInputStream input = null;        BufferedReader bufreader = null;        try {            input = new FileInputStream(file);            bufreader = new BufferedReader(new InputStreamReader(input));            String info = null;            while((info = bufreader.readLine()) != null) {                People people = Obj2Record(info);                arrs.add(people);            }        } catch(IOException e) {            e.printStackTrace();        } finally {            if(bufreader != null) {                try {                    bufreader.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if(input != null) {                try {                    input.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        int len = arrs.size();        People[] array = new People[len];        for(int i = 0; i < len; i++) {            array[i] = arrs.get(i);        }        return array;    }    /**将记录解析成对象*/    private People Obj2Record(String content) {        if(content == null || "".equals(content)) {            return null;        }        String strs[] = content.split(",");        People people = new People();        people.id = Integer.parseInt(strs[0]);        people.name = strs[1];        people.pwd = strs[2];        return people;    }    /**将对象拼接成记录*/    private String Record2Obj(People people) {        StringBuffer sb = new StringBuffer();        sb.append(people.id);        sb.append(",");        sb.append(people.name);        sb.append(",");        sb.append(people.pwd);        return sb.toString();    }     /**返回要操作的文件*/    private File getFile() {        File file = new File(filePath);        if(!file.exists())            try {                file.createNewFile();            } catch (IOException e) {                e.printStackTrace();            }        return file;    }    class People {        protected int id;        protected String name;        protected String pwd;        public People() {}        public People(int id, String name, String pwd) {            this.id = id;            this.name = name;            this.pwd = pwd;        }    }}//该片段来自于http://byrx.net
相关栏目:

用户点评