Java中文件創(chuàng)建于寫入內(nèi)容的常見方法
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等等,想必大家也都知道,這里就不再展示了。
以上就是Java中文件創(chuàng)建于寫入內(nèi)容的常見方法的詳細(xì)內(nèi)容,更多關(guān)于Java文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
對(duì)SpringMVC的@RequestParam的解釋
下面小編就為大家?guī)硪黄獙?duì)SpringMVC的@RequestParam的解釋。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
macbook中springboot的jmeter壓測(cè)示例
這篇文章主要介紹了macbook中springboot的jmeter壓測(cè)示例詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11
使用ServletInputStream在攔截器或過濾器中應(yīng)用后重寫
這篇文章主要介紹了使用ServletInputStream在攔截器或過濾器中應(yīng)用后重寫,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
淺談js文件引用方式及其同步執(zhí)行與異步執(zhí)行
下面小編就為大家?guī)硪黄獪\談js文件引用方式及其同步執(zhí)行與異步執(zhí)行。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
Spring Boot Actuator執(zhí)行器運(yùn)行原理詳解
這篇文章主要介紹了Spring Boot Actuator執(zhí)行器運(yùn)行原理詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03

