Java | 文本处理

import java.io.*;

public class TextProcessing {
    // Text Processing
    public static void main(String[] args) throws IOException {
        String filePath = "1.txt" ;
        File[] fileInputListsPath = new File("TxtDocument\\2512").listFiles();
        String fileOutputPath = "TxtDocument\\4.txt";

        createFile(filePath);
        writeFileAppend(filePath, "content1");
        writeFileOverlay(filePath, "over");
        if(fileInputListsPath != null) {
            fileCombine(fileInputListsPath, fileOutputPath);
        }

        readFile(filePath);
    }

    /**
     * 功能描述:创建文件
     * @param filePath
     */
    public static void createFile(String filePath) {
        try {
            File file = new File(filePath);
            if(!file.exists())
                file.createNewFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能描述:文本追加写入
     * @param filePath
     * @param Content
     */
    public static void writeFileAppend(String filePath, String Content) {
        FileWriter fileWriter;
        try {
            fileWriter = new FileWriter(filePath, true);
            fileWriter.write(Content);
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能描述:文本覆盖写入
     * @param filePath
     * @param Content
     */
    public static void writeFileOverlay(String filePath, String Content) {
        PrintWriter printWriter;
        try {
            printWriter = new PrintWriter(filePath);
            printWriter.write(Content);
            printWriter.flush();
            printWriter.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能描述:文件文本读取
     * @param filePath
     */
    public static void readFile(String filePath) {
        InputStreamReader inputStreamReader;
        BufferedReader bufferedReader;
        try {
            File file = new File(filePath);
            inputStreamReader = new InputStreamReader(new FileInputStream(file), "gbk");
            bufferedReader = new BufferedReader(inputStreamReader);
            String string;
            while((string = bufferedReader.readLine()) != null) {
                System.out.println(string);
            }
            bufferedReader.close();
            inputStreamReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能描述:txt文件合并
     * @param fileInputListsPath
     * @param fileOutputPath
     * @throws IOException
     */
    public static void fileCombine(File[] fileInputListsPath, String fileOutputPath) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileOutputPath));
        int fileCount = 0, folderCount = 0;

        for(File file : fileInputListsPath) {
            if(file.isFile()) {
                fileCount += 1;
                BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
                String line;
                while((line = bufferedReader.readLine()) != null) {
                    bufferedWriter.write(line);
                    bufferedWriter.newLine();
                }
                bufferedReader.close();
            } else {
                folderCount += 1;
            }
        }

        bufferedWriter.close();
        System.out.println(fileCount);
        System.out.println(folderCount);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值