Java中文件寫入內(nèi)容的幾種常見方法
在日常開發(fā)中,肯定離不開要和文件打交道,今天就簡單羅列一下平時(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,性能好,其次是代碼簡潔。
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)文章
Spring向頁面?zhèn)髦岛徒邮茼撁鎮(zhèn)鬟^來的參數(shù)詳解
這篇文章主要給大家介紹了關(guān)于Spring向頁面?zhèn)髦岛徒邮茼撁鎮(zhèn)鬟^來的參數(shù)的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-06-06
springboot項(xiàng)目mapper無法自動(dòng)裝配未找到?UserMapper?類型的Bean解決辦法
這篇文章給大家介紹了springboot項(xiàng)目mapper無法自動(dòng)裝配,未找到?‘userMapper‘?類型的?Bean解決辦法(含報(bào)錯(cuò)原因),文章通過圖文結(jié)合的方式介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-02-02
Spring AOP的核心原理和兩種實(shí)現(xiàn)方式全解析
本文介紹Spring AOP的核心原理和兩種實(shí)現(xiàn)方式全解析,通過本文的學(xué)習(xí),我們掌握了Spring AOP的核心原理和兩種實(shí)現(xiàn)方式,這里對(duì)兩種方式進(jìn)行對(duì)比,幫助你選擇合適的開發(fā)方案,感興趣的朋友跟隨小編一起看看吧2026-01-01
springboot整合JSR303校驗(yàn)功能實(shí)現(xiàn)代碼
這篇文章主要介紹了springboot整合JSR303校驗(yàn)功能實(shí)現(xiàn),JSR303校驗(yàn)方法有統(tǒng)一校驗(yàn)的需求,統(tǒng)一校驗(yàn)實(shí)現(xiàn)以及分組校驗(yàn),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01

