Java中文件寫(xiě)入內(nèi)容的幾種常見(jiàn)方法
在日常開(kāi)發(fā)中,肯定離不開(kāi)要和文件打交道,今天就簡(jiǎn)單羅列一下平時(shí)比較常用的創(chuàng)建文件并向文件中寫(xiě)入數(shù)據(jù)的幾種方式,也歡迎大家補(bǔ)充。
1. 使用NIO的Files工具類(lèi)
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 或者不寫(xiě)
*/
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);
//-----------------------------------------------------------------------//
//還可以通過(guò)Files.newBufferedWriter來(lái)創(chuàng)建文件并寫(xiě)入內(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í)際開(kāi)發(fā)中,如果涉及到文件的創(chuàng)建,我一般是首選Files.createFile或者Files.write這種方式,首先是使用了NIO,性能好,其次是代碼簡(jiǎn)潔。
2.通過(guò)commons-io的FileUtils工具類(lèi)
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.一行一行往文件里面寫(xiě)
*/
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.可以通過(guò)seek方法來(lái)實(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);
}
}
}
}以上就是我整理的操作文件的常用方法,尤其推薦第一種方式和第二種方式,其他沒(méi)有羅列的比如: FileOutputStream, FileWriter等等,想必大家也都知道,這里就不再展示了。更多相關(guān)Java 文件寫(xiě)入內(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-04
Java實(shí)現(xiàn)的圖像查看器完整實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)的圖像查看器,以完整實(shí)例形式較為詳細(xì)的分析了java處理圖片的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
SpringBatch數(shù)據(jù)寫(xiě)入實(shí)現(xiàn)
Spring Batch通過(guò)ItemWriter接口及其豐富的實(shí)現(xiàn),提供了強(qiáng)大的數(shù)據(jù)寫(xiě)入能力,本文主要介紹了SpringBatch數(shù)據(jù)寫(xiě)入實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2025-04-04
手動(dòng)編譯并運(yùn)行Java項(xiàng)目實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了手動(dòng)編譯并運(yùn)行Java項(xiàng)目實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
Java多線程之同步工具類(lèi)CountDownLatch
這篇文章主要介紹了Java多線程之同步工具類(lèi)CountDownLatch,CountDownLatch是一個(gè)同步工具類(lèi),它允許一個(gè)或多個(gè)線程一直等待,直到其他線程執(zhí)行完后再執(zhí)行。例如,應(yīng)用程序的主線程希望在負(fù)責(zé)啟動(dòng)框架服務(wù)的線程已經(jīng)啟動(dòng)所有框架服務(wù)之后執(zhí)行,下面一起來(lái)學(xué)習(xí)學(xué)習(xí)內(nèi)容吧2021-10-10
Java實(shí)現(xiàn)音頻轉(zhuǎn)文本的示例代碼(語(yǔ)音識(shí)別)
Java中實(shí)現(xiàn)音頻轉(zhuǎn)文本通常涉及使用專(zhuān)門(mén)的語(yǔ)音識(shí)別服務(wù),本文主要介紹了Java實(shí)現(xiàn)音頻轉(zhuǎn)文本的示例代碼(語(yǔ)音識(shí)別),具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05

