java 中创建并且写文件的几种方式(java io 基础)

java中创建文件的五种方法。

  • Files.newBufferedWriter(Java 8)
  • Files.write(Java 7 推荐)
  • PrintWriter
  • File.createNewFile
  • FileOutputStream.write(byte[] b) 管道流
1.Files.newBufferedWriter(Java8)

java8 提供的newBufferedWriter可以创建文件,并向文件内写入数据。可以通过追加写模式,向文件内追加内容。

@Test
void testCreateFile1() throws IOException {
   String fileName = "D:\\data\\test\\newFile.txt";

   Path path = Paths.get(fileName);
   // 使用newBufferedWriter创建文件并写文件
   // 这里使用了try-with-resources方法来关闭流,不用手动关闭
   try (BufferedWriter writer =
                   Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
      writer.write("Hello World -创建文件!!");
   }

   //追加写模式
   try (BufferedWriter writer =
                Files.newBufferedWriter(path,
                        StandardCharsets.UTF_8,
                        StandardOpenOption.APPEND)){
       writer.write("Hello World -字母哥!!");
   }
}
2. Files.write(Java7)

在这里插入图片描述

这种方式是实现文本文件简单读写最方便快捷的方式。

@Test
void testCreateFile2() throws IOException {
   String fileName = "D:\\data\\test\\newFile2.txt";

   // 从JDK1.7开始提供的方法
   // 使用Files.write创建一个文件并写入
   Files.write(Paths.get(fileName),
               "Hello World -创建文件!!".getBytes(StandardCharsets.UTF_8));

   // 追加写模式
   Files.write(
         Paths.get(fileName),
         "Hello World -字母哥!!".getBytes(StandardCharsets.UTF_8),
         StandardOpenOption.APPEND);
}

3. PrintWriter

PrintWriter是一个比较古老的文件创建及写入方式,从JDK1.5就已经存在了,比较有特点的是:PrintWriter的println方法,可以实现一行一行的写文件。

@Test
void testCreateFile3() throws IOException {
   String fileName = "D:\\data\\test\\newFile3.txt";

   // JSD 1.5开始就已经存在的方法
   try (PrintWriter writer = new PrintWriter(fileName, "UTF-8")) {
      writer.println("Hello World -创建文件!!");
      writer.println("Hello World -字母哥!!");
   }

   // Java 10进行了改进,支持使用StandardCharsets指定字符集
   /*try (PrintWriter writer = new PrintWriter(fileName, StandardCharsets.UTF_8)) {

      writer.println("first line!");
      writer.println("second line!");

   } */

}

4.File.createNewFile()

createNewFile()方法的功能相对就比较纯粹,只是创建文件不做文件写入操作。 返回true表示文件成功,返回 false表示文件已经存在.可以配合FileWriter 来完成文件的写操作。

@Test
void testCreateFile4() throws IOException {
   String fileName = "D:\\data\\test\\newFile4.txt";

   File file = new File(fileName);

   // 返回true表示文件成功
   // false 表示文件已经存在
   if (file.createNewFile()) {
      System.out.println("创建文件成功!");
   } else {
      System.out.println("文件已经存在不需要重复创建");
   }

   // 使用FileWriter写文件
   try (FileWriter writer = new FileWriter(file)) {
      writer.write("Hello World -创建文件!!");
   }

}

5.最原始的过滤(管道)流方法

你想去加上Buffer缓冲,你就嵌套一个BufferedWriter,你想去向文件中写java对象你就嵌套一个ObjectOutputStream。但归根结底要用到FileOutputStream。

@Test
void testCreateFile5() throws IOException {
   String fileName = "D:\\data\\test\\newFile5.txt";
   try(FileOutputStream fos = new FileOutputStream(fileName);
      OutputStreamWriter osw = new OutputStreamWriter(fos);
      BufferedWriter bw = new BufferedWriter(osw);){
      bw.write("Hello World -创建文件!!");
      bw.flush();
   }
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值