Java文件处理:Txt 文本创建、写入和读取

import java.io.*;

public class Txt {

    public static void main(String args[]) {
        createFile("E://123.txt");
        writeFileOverlay("E://123.txt", "123");
        writeFileAppend("E://123.txt", "456\r\n789");
        readFile("E://123.txt");
    }

    /**
     * 功能描述:
     * <创建文件>
     *
     * @param filePath 1
     * @return void
     * @author zhoulipu
     * @date 2019/8/2 16:23
     */
    private static void createFile(String filePath) {
        try {
            File file = new File(filePath);
            // 判断文件是否存在
            if (!file.exists()) {
                file.createNewFile();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能描述:
     * <追加写入>
     *
     * @param filePath 1
     * @param Content  2
     * @return void
     * @author zhoulipu
     * @date 2019/8/2 16:23
     */
    private static void writeFileAppend(String filePath, String Content) {
        FileWriter fw = null;
        try {
            fw = new FileWriter(filePath, true);
            fw.write(Content);
            fw.flush();
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fw != null) {
                    fw.flush();
                    fw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 功能描述:
     * <覆盖写入>
     *
     * @param filePath 1
     * @param Content  2
     * @return void
     * @author zhoulipu
     * @date 2019/8/2 16:23
     */
    private static void writeFileOverlay(String filePath, String Content) {
        PrintWriter pw = null;
        try {
            pw = new PrintWriter(filePath);
            pw.write(Content);
            pw.flush();
            pw.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (pw != null) {
                pw.flush();
                pw.close();
            }
        }
    }

    /**
     * 功能描述:
     * <读取文件>
     *
     * @param filePath 1
     * @return void
     * @author zhoulipu
     * @date 2019/8/2 16:24
     */
    private static void readFile(String filePath) {
        InputStreamReader in = null;
        BufferedReader br = null;
        try {
            File file = new File(filePath);
            in = new InputStreamReader(new FileInputStream(file), "gbk");
            br = new BufferedReader(in);
            String s;
            // 逐行读取
            while ((s = br.readLine()) != null) {
                System.out.println(s);
            }
            br.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值