ToB企服应用市场:ToB评测及商务社交产业平台

标题: JavaIO 文件的读取,写入,复制,压缩,解压等...相干利用,持续更新 [打印本页]

作者: 小秦哥    时间: 2024-8-30 15:24
标题: JavaIO 文件的读取,写入,复制,压缩,解压等...相干利用,持续更新
1. 文本文件的读取
文本的读取,返回值是一个list, 如果必要返回一整个string 在while循环中利用StringBuilder.append  即可
  1. /**
  2.      * 逐行读取文本
  3.      *
  4.      * @param filePath 文件路径
  5.      * @return List<String>
  6.      */
  7.     public static List<String> readTxtFile1(String filePath) throws IOException {
  8.         Path path = Paths.get(filePath);
  9.         //判断文件是否存在
  10.         if (!Files.exists(path)) {
  11.             log.error("file is not exist");
  12.             return null;
  13.         }
  14.         List<String> txtList = new ArrayList<>();
  15.         try (InputStreamReader read = new InputStreamReader(Files.newInputStream(path), StandardCharsets.UTF_8);
  16.              BufferedReader bufferedReader = new BufferedReader(read)) {
  17.             String lineTxt;
  18.             while (null != (lineTxt = bufferedReader.readLine())) {
  19.                 if (StringUtils.isNotEmpty(lineTxt)) {
  20.                     txtList.add(lineTxt);
  21.                 }
  22.             }
  23.         }
  24.         return txtList;
  25.     }
复制代码
2.文本文件的写入
  1. /**
  2. * 以指定的编码 写入数据
  3. */
  4. private static void outputStreamWriter(String filePath, List<String> content, Charset charset, boolean append) throws IOException {
  5.     try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(filePath, append), charset);
  6.          BufferedWriter bufferedWriter = new BufferedWriter(writer)) {
  7.         for (String item : content) {
  8.             bufferedWriter.write(item);
  9.             bufferedWriter.newLine();
  10.         }
  11.     }
  12. }
复制代码
3.文件的拷贝
  1. /**
  2. * 简单的文件拷贝,不使用缓冲区,适用于小文件
  3. *
  4. * @param sourceFile 源文件
  5. * @param targetFile 目标文件
  6. */
  7. public static void copyFile(String sourceFile, String targetFile) throws IOException {
  8.     try (FileInputStream fileInputStream = new FileInputStream(sourceFile);
  9.          FileOutputStream fileOutputStream = new FileOutputStream(targetFile)) {
  10.         byte[] b = new byte[1024];
  11.         int length;
  12.         while ((length = fileInputStream.read(b)) != -1) {
  13.             fileOutputStream.write(b, 0, length);
  14.             // 不能用 fileOutputStream.write(b) 因为最后有可能读不够而出错
  15.         }
  16.     }
  17. }
复制代码
4.大文件的拷贝
  1. /**
  2. * 进行文件的拷贝-高效
  3. * 使用字节处理流 字节缓冲输入流和字节缓冲输出流
  4. *
  5. * @param source 源
  6. * @param target 复制到
  7. * @return boolean 结果
  8. */
  9. public static boolean BufferedStreamFileCopy(String source, String target) {
  10.     if (StringUtils.isEmpty(source) || StringUtils.isEmpty(target)) {
  11.         log.error("文件路径不存在! path:{}", source);
  12.         return false;
  13.     }
  14.     if (source.equals(target)) {
  15.         log.error("复制的源文件和目标文件不能是同一个文件! path:{}", source);
  16.         return false;
  17.     }
  18.     Path path = Paths.get(source);
  19.     boolean exists = Files.exists(path);
  20.     if (!exists) {
  21.         log.error("文件不存在! path:{}", source);
  22.         return false;
  23.     }
  24.     long currentTimeMillis = System.currentTimeMillis();
  25.     try (BufferedInputStream bufferedInputStream = new BufferedInputStream(Files.newInputStream(path));
  26.          BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(Files.newOutputStream(Paths.get(target)))) {
  27.         byte[] bytes = new byte[1024];
  28.         int length;
  29.         while ((length = bufferedInputStream.read(bytes)) != -1) {
  30.             bufferedOutputStream.write(bytes, 0, length);
  31.         }
  32.         log.info("copy file success, time:{} ms", System.currentTimeMillis() - currentTimeMillis);
  33.         return true;
  34.     } catch (IOException e) {
  35.         log.error("BufferedStreamFileCopy 拷贝文件异常", e);
  36.         return false;
  37.     }
  38. }
复制代码
5.文本文件编码转换
  1. /**
  2. * 编码转换- 如一个文件的编码是 gb2312 转为 utf-8
  3. * 请注意,请用文件本身的正确的编码尝试读取,否则会乱码
  4. *
  5. * @param filePath    原始文件
  6. * @param oldCharset  原始字符编码
  7. * @param newFilePath 新文件
  8. * @param newCharset  新字符编码
  9. * @throws IOException io异常
  10. */
  11. private static void conversionCharset(String filePath, String oldCharset, String newFilePath, String newCharset) throws IOException {
  12.     try (InputStreamReader reader = new InputStreamReader(Files.newInputStream(Paths.get(filePath)), oldCharset);
  13.          BufferedReader bufferedReader = new BufferedReader(reader);
  14.          OutputStreamWriter outputStreamWriter = new OutputStreamWriter(Files.newOutputStream(Paths.get(newFilePath)), newCharset);
  15.          BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter)) {
  16.         String line;
  17.         while (null != (line = bufferedReader.readLine())) {
  18.             bufferedWriter.write(line);
  19.             bufferedWriter.newLine();
  20.         }
  21.     }
  22. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4