java write to files_Java 8 write to file example

Java 8示例将内容导入文件。 您可以在链接的博客文章中找到reading files using java 8 APIs示例。

1. Java 8 write to file using BufferedWriter

BufferedWriter用于将文本写入字符或字节流。 在打印字符之前,它将字符存储在缓冲区中并成束打印。 如果不进行缓冲,则每次调用print()方法都会导致将字符转换为字节,然后将这些字节立即写入文件中,这可能会非常低效。

write content to file using Java 8 API write content to file using Java 8程序是–//Get the file reference

Path path = Paths.get("c:/output.txt");

//Use try-with-resource to get auto-closeable writer instance

try (BufferedWriter writer = Files.newBufferedWriter(path))

{

writer.write("Hello World !!");

}

2. Write to file using Files.write()String content = "Hello World !!";

Files.write(Paths.get("c:/output.txt"), content.getBytes());

以上两种方法都适用于几乎所有需要用Java 8编写文件的用例。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值