本篇内容主要讲解“java创建写入文件的方式有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“java创建写入文件的方式有哪些”吧!
在本文中大量的使用到了try-with-resources语法,这个语法真的是很久的了,但是的确还有小伙伴不知道(知道的小伙伴就略过吧)。我还是说一下,下文中的管道流不是我没close,是自动关闭close的。
try(管道流、连接等实现了Closeable接口的类){
//这里使用类对象操作
}
//用try()包含起来,就不用在finally里面自己手动的去 Object.close()了,会自动的关闭
java8 提供的newBufferedWriter可以创建文件,并向文件内写入数据。可以通过追加写模式,向文件内追加内容。
@Test
void testCreateFile1() throws IOException {
String fileName = "D:\\data\\test\\newFile.txt";
Path path = Paths.get(fileName);
// 使用newBufferedWriter创建文件并写文件
// 这里使用了try-with-resources方法来关闭流,不用手动关闭
try (BufferedWriter writer =
Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
writer.write("Hello World -创建文件!!");
}
//追加写模式
try (BufferedWriter writer =
Files.newBufferedWriter(path,
StandardCharsets.UTF_8,
StandardOpenOption.APPEND)){
writer.write("Hello World -字母哥!!");
}
}
下面的这种方式Files.write,是笔者推荐的方式,语法简单,而且底层是使用Java NIO实现的。同样提供追加写模式向已经存在的文件种追加数据。这种方式是实现文本文件简单读写最方便快捷的方式。
@Test
void testCreateFile2() throws IOException {
String fileName = "D:\\data\\test\\newFile2.txt";
// 从JDK1.7开始提供的方法
// 使用Files.write创建一个文件并写入
Files.write(Paths.get(fileName),
"Hello World -创建文件!!".getBytes(StandardCharsets.UTF_8));
// 追加写模式
Files.write(
Paths.get(fileName),
"Hello World -字母哥!!".getBytes(StandardCharsets.UTF_8),
StandardOpenOption.APPEND);
}
PrintWriter是一个比较古老的文件创建及写入方式,从JDK1.5就已经存在了,比较有特点的是:PrintWriter的println方法,可以实现一行一行的写文件。
@Test
void testCreateFile3() throws IOException {
String fileName = "D:\\data\\test\\newFile3.txt";
// JSD 1.5开始就已经存在的方法
try (PrintWriter writer = new PrintWriter(fileName, "UTF-8")) {
writer.println("Hello World -创建文件!!");
writer.println("Hello World -字母哥!!");
}
// Java 10进行了改进,支持使用StandardCharsets指定字符集
/*try (PrintWriter writer = new PrintWriter(fileName, StandardCharsets.UTF_8)) {
writer.println("first line!");
writer.println("second line!");
} */
}
createNewFile()方法的功能相对就比较纯粹,只是创建文件不做文件写入操作。 返回true表示文件成功,返回 false表示文件已经存在.可以配合FileWriter 来完成文件的写操作。
@Test
void testCreateFile4() throws IOException {
String fileName = "D:\\data\\test\\newFile4.txt";
File file = new File(fileName);
// 返回true表示文件成功
// false 表示文件已经存在
if (file.createNewFile()) {
System.out.println("创建文件成功!");
} else {
System.out.println("文件已经存在不需要重复创建");
}
// 使用FileWriter写文件
try (FileWriter writer = new FileWriter(file)) {
writer.write("Hello World -创建文件!!");
}
}
最原始的方式就是使用管道流嵌套的方法,但是笔者觉得这种方法历久弥新,使用起来非常灵活。你想去加上Buffer缓冲,你就嵌套一个BufferedWriter,你想去向文件中写java对象你就嵌套一个ObjectOutputStream。但归根结底要用到FileOutputStream。
@Test
void testCreateFile5() throws IOException {
String fileName = "D:\\data\\test\\newFile5.txt";
try(FileOutputStream fos = new FileOutputStream(fileName);
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);){
bw.write("Hello World -创建文件!!");
bw.flush();
bw.close();
}
}
import org.apache.commons.io.FileUtils;
public static void saveTxt(String filePath, String fileName, String data){
try{
File folder = new File(filePath);
if(!folder.exists()){
folder.mkdirs();
}
File file = new File(filePath + "/" + fileName);
FileUtils.writeStringToFile(file,data,"UTF-8");
}catch(IOEException e){
log.error("error")
}
}
到此,相信大家对“java创建写入文件的方式有哪些”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。