txt文件读写工具类


package com.yqjr.nbs.car.af.ln.util;

import com.yqjr.nbs.car.af.ln.dto.InvoiceInfoDto;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


/**
 * txt文件读写工具类
 * @date 2018-03-29
 * 
*/
  
public class WriterAndReadUtil {

    /**
     * @param url 读取文件路径
    **/
    public List read(String url) throws Exception{
        File file = new File(url);
        //FileReader fr = null;
        InputStreamReader fr = null;
        BufferedReader br = null;
        List contents= new ArrayList();
        try {
            fr = new InputStreamReader(new FileInputStream(file), "UTF-8");
            br = new BufferedReader(fr);
            String line = br.readLine();
            while (line != null) {
                System.out.println(line);
                contents.add(line);
                line = br.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                br.close();
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return contents;
    }

    /**
     * @param contents 写入txt文件内容
     * @param url 文件存储路径
     * @param YYYYMM 文件每行内容中包含的时间
     */
    public static void write(List contents, String url, String YYYYMM) throws Exception{
        File file = new File(url);
        File parent = file.getParentFile(); 
        if(parent==null||!parent.exists()){ 
            parent.mkdirs(); 
        }
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
            fw = new FileWriter(file);
            bw = new BufferedWriter(fw);
            for (int i = 0; i < contents.size(); i++) {
                String content = contents.get(i).getRegisterno();
                content += "@|@";
                content += String.valueOf(contents.get(i).getCode());
                content += "@|@";
                content += YYYYMM;
                content += "@|@";
                content += contents.get(i).getEmail();
                bw.write(content);
                bw.newLine();// 光标换行
            }
            bw.flush();// 清空缓冲区内容
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                bw.close();
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 测试
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        WriterAndReadUtil writerAndReadUtil = new WriterAndReadUtil();
        String url = "E:\\read.txt";
        writerAndReadUtil.read(url);
        System.out.print("read");
        List arg = new ArrayList();
        InvoiceInfoDto l = new InvoiceInfoDto();
        l.setRegisterno("asdsads");
        l.setCode("456");
        arg.add(l);
        writerAndReadUtil.write(arg, "E:\\write.txt", "201803");
        System.out.println();
    }
}

你可能感兴趣的:(txt文件读写工具类)