Java中向文件寫入數(shù)據(jù)的幾種常見方式分享
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.如果文件不存在,則會創(chuàng)建文件
* 2.這種方式會使后面的內(nèi)容覆蓋前面的內(nèi)容
*/
Files.write(Paths.get(path), "hello world!!!".getBytes(StandardCharsets.UTF_8));
/**
* 1.如果文件不存在,則會創(chuàng)建文件
* 2.文件內(nèi)容追加,
*/
Files.write(Paths.get(path), "hello world!!!".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
/**
* 文件內(nèi)容追加. 注意:如果文件存在,則會報(bào)錯(cuò),可以使用 StandardOpenOption.CREATE 或者不寫
*/
Files.write(Paths.get(path), "hello world2222!!!".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND);
/**
* 1.如果文件不存在,則會創(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.如果文件不存在,會創(chuàng)建文件
* 2.文件內(nèi)容會覆蓋
*/
try(BufferedWriter bw = Files.newBufferedWriter(Paths.get(path), StandardCharsets.UTF_8)) {
bw.write("hello world!!!");
}
/**
* 1.如果文件不存在,會創(chuàng)建文件
* 2.文件內(nèi)容會追加
*/
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中向文件寫入數(shù)據(jù)的幾種常見方式分享的文章就介紹到這了,更多相關(guān)Java中向文件寫入數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
獲取Java的MyBatis框架項(xiàng)目中的SqlSession的方法
SqlSession中包括已經(jīng)映射好的SQL語句,這樣對象實(shí)例就可以直接拿過來用了,那么這里就來講解獲取Java的MyBatis框架項(xiàng)目中的SqlSession的方法2016-06-06
Mybatis-plus通用查詢方法封裝的實(shí)現(xiàn)
本文主要介紹了Mybatis-plus通用查詢方法封裝的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Jenkins集成sonarQube實(shí)現(xiàn)代碼質(zhì)量檢查過程圖解
這篇文章主要介紹了Jenkins集成sonarQube實(shí)現(xiàn)代碼質(zhì)量檢查過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
SpringBoot集成Elasticsearch過程實(shí)例
這篇文章主要介紹了SpringBoot集成Elasticsearch過程實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04

