Java中文件寫入內(nèi)容的幾種常見方法
在日常開發(fā)中,肯定離不開要和文件打交道,今天就簡(jiǎn)單羅列一下平時(shí)比較常用的創(chuàng)建文件并向文件中寫入數(shù)據(jù)的幾種方式,也歡迎大家補(bǔ)充。
1. 使用NIO的Files工具類
import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.Collections; /** * @author fu yuan hui * @date 2023-10-24 19:39:11 Tuesday */ public class FileTest { public static void main(String[] args) throws Exception{ String path = "D:/test/java.txt"; //Files.createFile(Paths.get(path)); /** * 1.如果文件不存在,則會(huì)創(chuàng)建文件 * 2.這種方式會(huì)使后面的內(nèi)容覆蓋前面的內(nèi)容 */ Files.write(Paths.get(path), "hello world!!!".getBytes(StandardCharsets.UTF_8)); /** * 1.如果文件不存在,則會(huì)創(chuàng)建文件 * 2.文件內(nèi)容追加, */ Files.write(Paths.get(path), "hello world!!!".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND); /** * 文件內(nèi)容追加. 注意:如果文件存在,則會(huì)報(bào)錯(cuò),可以使用 StandardOpenOption.CREATE 或者不寫 */ Files.write(Paths.get(path), "hello world2222!!!".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND); /** * 1.如果文件不存在,則會(huì)創(chuàng)建文件 * 2.文件內(nèi)容追加 * 3.第二個(gè)參數(shù)是集合,可以實(shí)現(xiàn)“換行”追加 */ Files.write(Paths.get(path), Collections.singleton("hello world333!"), StandardCharsets.UTF_8, StandardOpenOption.APPEND); //-----------------------------------------------------------------------// //還可以通過Files.newBufferedWriter來創(chuàng)建文件并寫入內(nèi)容 /** * 1.如果文件不存在,會(huì)創(chuàng)建文件 * 2.文件內(nèi)容會(huì)覆蓋 */ try(BufferedWriter bw = Files.newBufferedWriter(Paths.get(path), StandardCharsets.UTF_8)) { bw.write("hello world!!!"); } /** * 1.如果文件不存在,會(huì)創(chuàng)建文件 * 2.文件內(nèi)容會(huì)追加 */ try(BufferedWriter bw = Files.newBufferedWriter(Paths.get(path), StandardCharsets.UTF_8, StandardOpenOption.APPEND)) { bw.write("hello world222!!!"); } } }
在實(shí)際開發(fā)中,如果涉及到文件的創(chuàng)建,我一般是首選Files.createFile或者Files.write這種方式,首先是使用了NIO,性能好,其次是代碼簡(jiǎn)潔。
2.通過commons-io的FileUtils工具類
import org.apache.commons.io.FileUtils; import java.io.File; import java.nio.charset.StandardCharsets; /** * @author fu yuan hui * @date 2023-10-24 19:39:11 Tuesday */ public class FileTest { public static void main(String[] args) throws Exception{ String path = "D:/test/java3.txt"; /** * 1.如果文件不存在,則創(chuàng)建文件 * 2.最后一個(gè)參數(shù)為true,表示追加 */ FileUtils.writeStringToFile(new File(path), "hello world999", StandardCharsets.UTF_8, true); } }
3.使用BufferedWriter
package com.efreight.oss.transfer.handler; import java.io.BufferedWriter; import java.io.FileWriter; /** * @author fu yuan hui * @date 2023-10-24 19:39:11 Tuesday */ public class FileTest { public static void main(String[] args) throws Exception{ String path = "D:/test/java3.txt"; /** * 1.如果文件不存在,則創(chuàng)建文件 * 2.第二個(gè)參數(shù)為true:表示文件內(nèi)容是追加 */ try(BufferedWriter bf = new BufferedWriter(new FileWriter(path, true))) { bf.write("hello world666"); } } }
4.使用 PrintWriter
import java.io.PrintWriter; import java.nio.charset.StandardCharsets; /** * @author fu yuan hui * @date 2023-10-24 19:39:11 Tuesday */ public class FileTest { public static void main(String[] args) throws Exception{ String path = "D:/test/java3.txt"; /** * 1.如果文件不存在,則創(chuàng)建文件 * 2.一行一行往文件里面寫 */ try(PrintWriter printWriter = new PrintWriter(path, StandardCharsets.UTF_8.name())) { printWriter.println("hello world111"); printWriter.println("hello world222"); printWriter.write("today is very hot"); } } }
5.使用 RandomAccessFile
import java.io.RandomAccessFile; /** * @author fu yuan hui * @date 2023-10-24 19:39:11 Tuesday */ public class FileTest { public static void main(String[] args) throws Exception{ String path = "D:/test/java4.txt"; /** * 1.如果文件不存在,則創(chuàng)建文件 * 2.可以通過seek方法來實(shí)現(xiàn)內(nèi)容的追加 */ for (int i = 0; i < 5; i++) { try(RandomAccessFile rf = new RandomAccessFile(path, "rw")) { rf.seek(rf.length()); rf.writeBytes("hello RandomAccessFile: " + i); } } } }
以上就是我整理的操作文件的常用方法,尤其推薦第一種方式和第二種方式,其他沒有羅列的比如: FileOutputStream, FileWriter等等,想必大家也都知道,這里就不再展示了。更多相關(guān)Java 文件寫入內(nèi)容內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ElasticSearch之索引模板滾動(dòng)索引實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了ElasticSearch之索引模板滾動(dòng)索引實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Java實(shí)現(xiàn)的圖像查看器完整實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)的圖像查看器,以完整實(shí)例形式較為詳細(xì)的分析了java處理圖片的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10SpringBatch數(shù)據(jù)寫入實(shí)現(xiàn)
Spring Batch通過ItemWriter接口及其豐富的實(shí)現(xiàn),提供了強(qiáng)大的數(shù)據(jù)寫入能力,本文主要介紹了SpringBatch數(shù)據(jù)寫入實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2025-04-04手動(dòng)編譯并運(yùn)行Java項(xiàng)目實(shí)現(xiàn)過程解析
這篇文章主要介紹了手動(dòng)編譯并運(yùn)行Java項(xiàng)目實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10Java實(shí)現(xiàn)音頻轉(zhuǎn)文本的示例代碼(語音識(shí)別)
Java中實(shí)現(xiàn)音頻轉(zhuǎn)文本通常涉及使用專門的語音識(shí)別服務(wù),本文主要介紹了Java實(shí)現(xiàn)音頻轉(zhuǎn)文本的示例代碼(語音識(shí)別),具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05