【深入Java基础】Java IO 使用

一个常用的读取和写入txt的工具类

package util;

import java.io.*;

/**
 * @author
 * wxg
 */
public class TxtUtil {
    /**
     * 读取txt文件
     * @param path
     * 文件路径
     * @return
     */
    public StringBuilder readTxt(String path){
        return readTxt2(path,"gbk");
    }

    /**
     * 读取txt文件
     * @param path
     * 文件路径
     * @param encoding
     * 文件编码
     * @return
     */
    public StringBuilder readTxt(String path,String encoding){
        return readTxt2(path,encoding);
    }
    private StringBuilder readTxt1(String path){
        //判断文件是否存在
        File file = new File(path);
        if (!file.exists()) {
            return null;
        }
        try {
            Reader reader = new FileReader(path);//Reader : 字符流
            BufferedReader br = new BufferedReader(reader);//BufferedReader : 缓冲输入流,效率高
            StringBuilder sb = new StringBuilder();//StringBuilder : 效率比String高,非线程安全,StringBuffer是线程安全的。
            //存在中文乱码问题
            String str;
            while ((str = br.readLine())!=null){
                sb.append(str).append("\r\n");
            }
            reader.close();
            br.close();
            return sb;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    private StringBuilder readTxt2(String path,String encoding){
        //判断文件是否存在
        File file = new File(path);
        if (!file.exists()) {
            return null;
        }
        try {
            InputStream is = new FileInputStream(path);//文件字节输入流
            InputStreamReader isr = new InputStreamReader(is,encoding);//InputStreamReader : 字符输入流,指定编码
            BufferedReader br = new BufferedReader(isr);//缓冲字符流
            StringBuilder sb = new StringBuilder();
            String str;
            while ((str = br.readLine())!=null){
                sb.append(str).append("\r\n");
            }
            br.close();
            isr.close();
            is.close();
            return sb;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public void writeTxt(String text,String path){
        writeTxt(text,path,false,"gbk");
    }
    public void writeTxt(String text,String path,boolean append){
        writeTxt(text,path,append,"gbk");
    }
    public void writeTxt(String text,String path,boolean append, String encoding) {
        //判断文件是否存在
        File file = new File(path);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            //以指定编码的方式写文件,并选择是否追加
            OutputStream os = new FileOutputStream(path, append);
            OutputStreamWriter osw = new OutputStreamWriter(os, encoding);
            BufferedWriter bw = new BufferedWriter(osw);
            bw.write(text);
            bw.newLine();
            bw.close();
            osw.close();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

勇敢牛牛_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值