1. 文本文件的读取

文本的读取,返回值是一个list, 如果需要返回一整个string 在while循环中使用StringBuilder.append  即可

/**
     * 逐行读取文本
     *
     * @param filePath 文件路径
     * @return List<String>
     */
    public static List<String> readTxtFile1(String filePath) throws IOException {
        Path path = Paths.get(filePath);
        //判断文件是否存在
        if (!Files.exists(path)) {
            log.error("file is not exist");
            return null;
        }
        List<String> txtList = new ArrayList<>();
        try (InputStreamReader read = new InputStreamReader(Files.newInputStream(path), StandardCharsets.UTF_8);
             BufferedReader bufferedReader = new BufferedReader(read)) {
            String lineTxt;
            while (null != (lineTxt = bufferedReader.readLine())) {
                if (StringUtils.isNotEmpty(lineTxt)) {
                    txtList.add(lineTxt);
                }
            }
        }
        return txtList;
    }

2.文本文件的写入

/**
 * 以指定的编码 写入数据
 */
private static void outputStreamWriter(String filePath, List<String> content, Charset charset, boolean append) throws IOException {
    try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(filePath, append), charset);
         BufferedWriter bufferedWriter = new BufferedWriter(writer)) {
        for (String item : content) {
            bufferedWriter.write(item);
            bufferedWriter.newLine();
        }
    }
}

3.文件的拷贝

/**
 * 简单的文件拷贝,不使用缓冲区,适用于小文件
 *
 * @param sourceFile 源文件
 * @param targetFile 目标文件
 */
public static void copyFile(String sourceFile, String targetFile) throws IOException {
    try (FileInputStream fileInputStream = new FileInputStream(sourceFile);
         FileOutputStream fileOutputStream = new FileOutputStream(targetFile)) {
        byte[] b = new byte[1024];
        int length;
        while ((length = fileInputStream.read(b)) != -1) {
            fileOutputStream.write(b, 0, length);
            // 不能用 fileOutputStream.write(b) 因为最后有可能读不够而出错
        }
    }
}

4.大文件的拷贝

/**
 * 进行文件的拷贝-高效
 * 使用字节处理流 字节缓冲输入流和字节缓冲输出流
 *
 * @param source 源
 * @param target 复制到
 * @return boolean 结果
 */
public static boolean BufferedStreamFileCopy(String source, String target) {
    if (StringUtils.isEmpty(source) || StringUtils.isEmpty(target)) {
        log.error("文件路径不存在! path:{}", source);
        return false;
    }

    if (source.equals(target)) {
        log.error("复制的源文件和目标文件不能是同一个文件! path:{}", source);
        return false;
    }

    Path path = Paths.get(source);
    boolean exists = Files.exists(path);
    if (!exists) {
        log.error("文件不存在! path:{}", source);
        return false;
    }
    long currentTimeMillis = System.currentTimeMillis();
    try (BufferedInputStream bufferedInputStream = new BufferedInputStream(Files.newInputStream(path));
         BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(Files.newOutputStream(Paths.get(target)))) {
        byte[] bytes = new byte[1024];
        int length;
        while ((length = bufferedInputStream.read(bytes)) != -1) {
            bufferedOutputStream.write(bytes, 0, length);
        }
        log.info("copy file success, time:{} ms", System.currentTimeMillis() - currentTimeMillis);
        return true;
    } catch (IOException e) {
        log.error("BufferedStreamFileCopy 拷贝文件异常", e);
        return false;
    }
}

5.文本文件编码转换

/**
 * 编码转换- 如一个文件的编码是 gb2312 转为 utf-8
 * 请注意,请用文件本身的正确的编码尝试读取,否则会乱码
 *
 * @param filePath    原始文件
 * @param oldCharset  原始字符编码
 * @param newFilePath 新文件
 * @param newCharset  新字符编码
 * @throws IOException io异常
 */
private static void conversionCharset(String filePath, String oldCharset, String newFilePath, String newCharset) throws IOException {
    try (InputStreamReader reader = new InputStreamReader(Files.newInputStream(Paths.get(filePath)), oldCharset);
         BufferedReader bufferedReader = new BufferedReader(reader);
         OutputStreamWriter outputStreamWriter = new OutputStreamWriter(Files.newOutputStream(Paths.get(newFilePath)), newCharset);
         BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter)) {
        String line;
        while (null != (line = bufferedReader.readLine())) {
            bufferedWriter.write(line);
            bufferedWriter.newLine();
        }
    }
}