前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java文件写入的6种方法

Java文件写入的6种方法

作者头像
bisal
发布2021-11-01 11:15:42
1.8K0
发布2021-11-01 11:15:42
举报

写程序时经常会碰到读写文件的场景,在Java中操作文件的方法本质上只有两种:字符流和字节流,而他们的实现类又有很多,因此,有时候用起来,就会比较乱。

这篇文章系统介绍了Java操作文件的几种方式,学习一下,

https://www.cnblogs.com/rinack/p/14173936.html

1baa57d2c74472a3f60337ad10a34721.png
1baa57d2c74472a3f60337ad10a34721.png
b807b7acd1c1f7f5c9b2b70558e48b59.png
b807b7acd1c1f7f5c9b2b70558e48b59.png
e74c47e3b968c063c98cd3ca619b6e89.png
e74c47e3b968c063c98cd3ca619b6e89.png
1cfed231c156d981a8466e812423ddb0.png
1cfed231c156d981a8466e812423ddb0.png
ef665f569817a71e515025db4cae7685.png
ef665f569817a71e515025db4cae7685.png
37dc15cd877a390015159aa2455eb3bc.png
37dc15cd877a390015159aa2455eb3bc.png
918245815ea25e29576bb6467841862c.png
918245815ea25e29576bb6467841862c.png
679bb82138dc7a3a41f934bdca276a34.png
679bb82138dc7a3a41f934bdca276a34.png
a77557c1beb6755b458c7a7499df5f5f.png
a77557c1beb6755b458c7a7499df5f5f.png
e6f0ef87fa7dce5c195649c7ec18e2a2.png
e6f0ef87fa7dce5c195649c7ec18e2a2.png

FileWriter类的实现如下,

代码语言:javascript
复制
/** 
  * 方法 1:使用 FileWriter 写文件 
  * @param filepath 文件目录 
  * @param content  待写入内容 
  * @throws IOException 
  */ 
public static void fileWriterMethod(String filepath, String content) throws IOException { 
    try (FileWriter fileWriter = new FileWriter(filepath)) { 
        fileWriter.append(content); 
    } 
}

只需要传入具体的文件路径和待写入的内容即可,调用代码如下,

代码语言:javascript
复制
public static void main(String[] args) { 
    fileWriterMethod("/Users/mac/Downloads/io_test/write1.txt", "Hello, Java."); 
}
130d3da7d6c50656ee7ab99ff15010ba.png
130d3da7d6c50656ee7ab99ff15010ba.png

了解了缓存区的优点之后,咱们回到本文的主题,接下来我们用BufferedWriter来文件的写入,实现代码如下,

代码语言:javascript
复制
/** 
 * 方法 2:使用 BufferedWriter 写文件 
 * @param filepath 文件目录 
 * @param content  待写入内容 
 * @throws IOException 
 */ 
public static void bufferedWriterMethod(String filepath, String content) throws IOException { 
    try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filepath))) { 
        bufferedWriter.write(content); 
    } 
}
797a9fffb0a381c64617ac7449ab9236.png
797a9fffb0a381c64617ac7449ab9236.png
代码语言:javascript
复制
/** 
 * 方法 3:使用 PrintWriter 写文件 
 * @param filepath 文件目录 
 * @param content  待写入内容 
 * @throws IOException 
 */ 
public static void printWriterMethod(String filepath, String content) throws IOException { 
    try (PrintWriter printWriter = new PrintWriter(new FileWriter(filepath))) { 
        printWriter.print(content); 
    } 
}
1f53581858068143eff1dd71eeb414da.png
1f53581858068143eff1dd71eeb414da.png
代码语言:javascript
复制
/** 
 * 方法 4:使用 FileOutputStream 写文件 
 * @param filepath 文件目录 
 * @param content  待写入内容 
 * @throws IOException 
 */ 
public static void fileOutputStreamMethod(String filepath, String content) throws IOException { 
    try (FileOutputStream fileOutputStream = new FileOutputStream(filepath)) { 
        byte[] bytes = content.getBytes(); 
        fileOutputStream.write(bytes); 
    } 
}
94d3061ecab1f94e1d7c2a5768580113.png
94d3061ecab1f94e1d7c2a5768580113.png
代码语言:javascript
复制
/** 
 * 方法 5:使用 BufferedOutputStream 写文件 
 * @param filepath 文件目录 
 * @param content  待写入内容 
 * @throws IOException 
 */ 
public static void bufferedOutputStreamMethod(String filepath, String content) throws IOException { 
    try (BufferedOutputStream bufferedOutputStream = new BufferedOutputStream( 
            new FileOutputStream(filepath))) { 
        bufferedOutputStream.write(content.getBytes()); 
    } 
}
4673ca0210ea87fe6490e8639b493e9d.png
4673ca0210ea87fe6490e8639b493e9d.png
代码语言:javascript
复制
/** 
 * 方法 6:使用 Files 写文件 
 * @param filepath 文件目录 
 * @param content  待写入内容 
 * @throws IOException 
 */ 
public static void filesTest(String filepath, String content) throws IOException { 
    Files.write(Paths.get(filepath), content.getBytes()); 
}
5aed868fc1a0fe02a8a098c139a50ff0.png
5aed868fc1a0fe02a8a098c139a50ff0.png
代码语言:javascript
复制
import java.io.*; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
 
public class WriteExample { 
    public static void main(String[] args) throws IOException { 
        // 构建写入内容 
        StringBuilder stringBuilder = new StringBuilder(); 
        for (int i = 0; i < 1000000; i++) { 
            stringBuilder.append("ABCDEFGHIGKLMNOPQRSEUVWXYZ"); 
        } 
        // 写入内容 
        final String content = stringBuilder.toString(); 
        // 存放文件的目录 
        final String filepath1 = "/Users/mac/Downloads/io_test/write1.txt"; 
        final String filepath2 = "/Users/mac/Downloads/io_test/write2.txt"; 
        final String filepath3 = "/Users/mac/Downloads/io_test/write3.txt"; 
        final String filepath4 = "/Users/mac/Downloads/io_test/write4.txt"; 
        final String filepath5 = "/Users/mac/Downloads/io_test/write5.txt"; 
        final String filepath6 = "/Users/mac/Downloads/io_test/write6.txt"; 
 
        // 方法一:使用 FileWriter 写文件 
        long stime1 = System.currentTimeMillis(); 
        fileWriterTest(filepath1, content); 
        long etime1 = System.currentTimeMillis(); 
        System.out.println("FileWriter 写入用时:" + (etime1 - stime1)); 
 
        // 方法二:使用 BufferedWriter 写文件 
        long stime2 = System.currentTimeMillis(); 
        bufferedWriterTest(filepath2, content); 
        long etime2 = System.currentTimeMillis(); 
        System.out.println("BufferedWriter 写入用时:" + (etime2 - stime2)); 
 
        // 方法三:使用 PrintWriter 写文件 
        long stime3 = System.currentTimeMillis(); 
        printWriterTest(filepath3, content); 
        long etime3 = System.currentTimeMillis(); 
        System.out.println("PrintWriterTest 写入用时:" + (etime3 - stime3)); 
 
        // 方法四:使用 FileOutputStream  写文件 
        long stime4 = System.currentTimeMillis(); 
        fileOutputStreamTest(filepath4, content); 
        long etime4 = System.currentTimeMillis(); 
        System.out.println("FileOutputStream 写入用时:" + (etime4 - stime4)); 
 
        // 方法五:使用 BufferedOutputStream 写文件 
        long stime5 = System.currentTimeMillis(); 
        bufferedOutputStreamTest(filepath5, content); 
        long etime5 = System.currentTimeMillis(); 
        System.out.println("BufferedOutputStream 写入用时:" + (etime5 - stime5)); 
 
        // 方法六:使用 Files 写文件 
        long stime6 = System.currentTimeMillis(); 
        filesTest(filepath6, content); 
        long etime6 = System.currentTimeMillis(); 
        System.out.println("Files 写入用时:" + (etime6 - stime6)); 


    } 
 
    /** 
     * 方法六:使用 Files 写文件 
     * @param filepath 文件目录 
     * @param content  待写入内容 
     * @throws IOException 
     */ 
    private static void filesTest(String filepath, String content) throws IOException { 
        Files.write(Paths.get(filepath), content.getBytes()); 
    } 
 
    /** 
     * 方法五:使用 BufferedOutputStream 写文件 
     * @param filepath 文件目录 
     * @param content  待写入内容 
     * @throws IOException 
     */ 
    private static void bufferedOutputStreamTest(String filepath, String content) throws IOException { 
        try (BufferedOutputStream bufferedOutputStream = new BufferedOutputStream( 
                new FileOutputStream(filepath))) { 
            bufferedOutputStream.write(content.getBytes()); 
        } 
    } 
 
    /** 
     * 方法四:使用 FileOutputStream  写文件 
     * @param filepath 文件目录 
     * @param content  待写入内容 
     * @throws IOException 
     */ 
    private static void fileOutputStreamTest(String filepath, String content) throws IOException { 
        try (FileOutputStream fileOutputStream = new FileOutputStream(filepath)) { 
            byte[] bytes = content.getBytes(); 
            fileOutputStream.write(bytes); 
        } 
    } 
 
    /** 
     * 方法三:使用 PrintWriter 写文件 
     * @param filepath 文件目录 
     * @param content  待写入内容 
     * @throws IOException 
     */ 
    private static void printWriterTest(String filepath, String content) throws IOException { 
        try (PrintWriter printWriter = new PrintWriter(new FileWriter(filepath))) { 
            printWriter.print(content); 
        } 
    } 
 
    /** 
     * 方法二:使用 BufferedWriter 写文件 
     * @param filepath 文件目录 
     * @param content  待写入内容 
     * @throws IOException 
     */ 
    private static void bufferedWriterTest(String filepath, String content) throws IOException { 
        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filepath))) { 
            bufferedWriter.write(content); 
        } 
    } 
 
    /** 
     * 方法一:使用 FileWriter 写文件 
     * @param filepath 文件目录 
     * @param content  待写入内容 
     * @throws IOException 
     */ 
    private static void fileWriterTest(String filepath, String content) throws IOException { 
        try (FileWriter fileWriter = new FileWriter(filepath)) { 
            fileWriter.append(content); 
        } 
    } 
}

在查看结果之前,我们先去对应的文件夹看看写入的文件是否正常,如下图所示,

0515e9e661b62be65c7c59be08266a26.png
0515e9e661b62be65c7c59be08266a26.png

从上述结果可以看出,每种方法都正常写入了26 MB的数据,他们最终执行的结果如下图所示,

e5e52fee47a813c41934dd1b426ef784.png
e5e52fee47a813c41934dd1b426ef784.png
4c29b9e9cb7bfca2a258fc47772830d5.png
4c29b9e9cb7bfca2a258fc47772830d5.png
7f90c2fd7d9c404880691587df008302.png
7f90c2fd7d9c404880691587df008302.png
80648265fbe253ffa79c4ec5feb39c17.png
80648265fbe253ffa79c4ec5feb39c17.png
c70e7074f142a2ba2fa7b79134533630.png
c70e7074f142a2ba2fa7b79134533630.png

cc8d9b4ed0e99a3118db8982d0fa53fe.png
cc8d9b4ed0e99a3118db8982d0fa53fe.png
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-10-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档