Java中的stream之文件和stream之间的转换

字节流

读取文件内容

 public static String readFileByStream(String filePath) {
        try (FileInputStream fileInputStream = new FileInputStream(filePath);
             BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) {
            return IOUtils.toString(bufferedInputStream, StandardCharsets.UTF_8);
        } catch (IOException e) {
            log.error("Failed to read file");
        }
        return null;
    }

写入文件(FileOutputStream无缓冲区)

 public static void writeFileByFileOutputStream(String filePath, String context) {
        try (FileOutputStream fileOutputStream = new FileOutputStream(filePath)) {
            //无缓冲区
            byte[] bytes = context.getBytes(StandardCharsets.UTF_8);
            int i = 0;
            while (i < bytes.length - 1024) {
                fileOutputStream.write(Arrays.copyOfRange(bytes, i, i + 1024));
                i += 1024;
            }
            fileOutputStream.write(Arrays.copyOfRange(bytes, i, bytes.length));
            fileOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

写入文件(BufferedOutputStream有缓冲区)

  public static void writeFileByBufferedOutputStream(String filePath, String context) {
        try (FileOutputStream fileOutputStream = new FileOutputStream(filePath);
             BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream)) {
            //有缓冲区
            byte[] bytes = context.getBytes(StandardCharsets.UTF_8);
            int i = 0;
            while (i < bytes.length - 1024) {
                fileOutputStream.write(Arrays.copyOfRange(bytes, i, i + 1024));
                i += 1024;
            }
            bufferedOutputStream.write(Arrays.copyOfRange(bytes, i, bytes.length));
            bufferedOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

测试

    @Test
    public void test_file_stream() {
        URL dataFileUrl = this.getClass().getClassLoader().getResource("files");
        String data = StreamUtil.readFileByStream(dataFileUrl.getPath() + File.separator + "data.txt");
        System.out.println(data);
        StreamUtil.writeFileByFileOutputStream(dataFileUrl.getPath() + File.separator + "data1.txt", data);
        StreamUtil.writeFileByBufferedOutputStream(dataFileUrl.getPath() + File.separator + "data2.txt", data);
    }

字符流

读取文件(BufferedReader)

public static String readFileByFileReader(String filePath) {
        try (FileReader fileReader = new FileReader(filePath);
             BufferedReader bufferedReader = new BufferedReader(fileReader)) {
            return bufferedReader.lines().collect(Collectors.joining("\n"));
        } catch (IOException e) {
            log.error("Failed to read file");
        }
        return null;
    }

逐行读取文件

public static String readFileByFileReaderEachLine(String filePath) {
        try (FileReader fileReader = new FileReader(filePath);
             BufferedReader bufferedReader = new BufferedReader(fileReader)) {
            int len = 0;
            StringBuffer sb = new StringBuffer();
            while ((len = bufferedReader.read()) != -1) {
                sb.append(bufferedReader.readLine());
                sb.append("\n");
            }
            return sb.toString();
        } catch (IOException e) {
            log.error("Failed to read file");
        }
        return null;
    }

写入文件(BufferedWriter)

public static void writeFileByFileWriter(String filePath, String context) {
        try (FileOutputStream stream = new FileOutputStream(filePath);
             OutputStreamWriter writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8);
             BufferedWriter bufferedWriter = new BufferedWriter(writer)) {
            bufferedWriter.write(context);
            bufferedWriter.newLine();//另起一行
            bufferedWriter.write("success");
            bufferedWriter.flush();//写入文件
        } catch (IOException e) {
            log.error("Failed to write into file", e);
        }
    }

测试

    @Test
    public void test_file_reader() {
        URL dataFileUrl = this.getClass().getClassLoader().getResource("files");
        String data = StreamUtil.readFileByFileReader(dataFileUrl.getPath() + File.separator + "data.txt");
        System.out.println(data);
        StreamUtil.writeFileByFileWriter(dataFileUrl.getPath() + File.separator + "data3.txt", data);
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值