如何在 Java 中创建文件并向其写入数据
本文讨论了在 Java 中创建文本文件并向其写入数据的方法。
Java 有多种创建文件的方法。需要记住的是,try-catch
块几乎是所有创建文件方法所必需的,这样任何 IO 异常都会被优雅地处理。不要忘记使用它。
使用 PrintWriter 创建 Java 文件
Java 中的 PrintWriter
允许你用指定的编码创建一个新文件。下面来举一个例子。
import java.io.*;
public class Main {
public static void main(String[] args) {
String fileName = "my-file.txt";
String encoding = "UTF-8";
try{
PrintWriter writer = new PrintWriter(fileName, encoding);
writer.println("The first line");
writer.println("The second line");
writer.close();
}
catch (IOException e){
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
在 Java 中用 Files
创建文件
我们也可以使用 java.nio.file
来创建一个文件,并向其中写入数据。下面的例子说明了这种方法。
import java.io.*;
import java.util.*;
import java.nio.file.*;
import java.nio.charset.*;
public class Main {
public static void main(String[] args) {
String fileName = "my-file.txt";
try{
List<String> lines = Arrays.asList("The first line", "The second line");
Path file = Paths.get(fileName);
Files.write(file, lines, StandardCharsets.UTF_8);
}
catch (IOException e){
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
在这个例子中,我们将 ArrayList
中的数据写入文件,这样 ArrayList
中的每一个元素都被写入输出文件中的新行。输出文件如下所示:
> The first line
> The second line
在 Java 中用 BufferedWriter
创建文件
下面的例子说明了使用 BufferedWriter
创建并写入一个文件。
import java.io.*;
import java.nio.charset.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Charset utf8 = StandardCharsets.UTF_8;
List<String> list = Arrays.asList("This is the first line", "This is the second line");
String content = "This is my content";
try
{
//File name with path
String fileName = "my-file.txt";
File myFile = new File(fileName);
FileOutputStream fos = new FileOutputStream(myFile);
OutputStreamWriter osw = new OutputStreamWriter(fos);
Writer writer = new BufferedWriter(osw);
//Write data using a String variable
writer.write(content + "\n");
//Write data from an ArrayList
for (String s : list) {
writer.write(s + "\n");
}
}
catch (IOException e){
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
输出文件如下所示:
> This is my content
> This is the first line
> This is the second line
在 Java 中用 FileWriter
创建文件
另一种在 Java 中创建新文件并写入文件的常用方法是使用 FileWriter
。
import java.io.*;
public class Main {
public static void main(String[] args) {
try {
FileWriter myWriter = new FileWriter("my-file.txt");
myWriter.write("We can also create a file and write to it using FileWriter");
myWriter.close();
}
catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
输出文件如下所示:
> We can also create a file and write to it using FileWriter
相关文章
如何在 Java 中延迟几秒钟的时间
发布时间:2023/12/17 浏览次数:217 分类:Java
-
本篇文章主要介绍如何在 Java 中制造程序延迟。本教程介绍了如何在 Java 中制造程序延时,并列举了一些示例代码来了解它。
如何在 Java 中把 Hashmap 转换为 JSON 对象
发布时间:2023/12/17 浏览次数:187 分类:Java
-
它描述了允许我们将哈希图转换为简单的 JSON 对象的方法。本文介绍了在 Java 中把 Hashmap 转换为 JSON 对象的方法。我们将看到关于创建一个 hashmap,然后将其转换为 JSON 对象的详细例子。
如何在 Java 中按值排序 Map
发布时间:2023/12/17 浏览次数:171 分类:Java
-
本文介绍了如何在 Java 中按值对 Map 进行排序。本教程介绍了如何在 Java 中按值对 Map
进行排序,并列出了一些示例代码来理解它。
如何在 Java 中打印 HashMap
发布时间:2023/12/17 浏览次数:192 分类:Java
-
本帖介绍了如何在 Java 中打印 HashMap。本教程介绍了如何在 Java 中打印 HashMap 元素,还列举了一些示例代码来理解这个主题。
在 Java 中更新 Hashmap 的值
发布时间:2023/12/17 浏览次数:146 分类:Java
-
本文介绍了如何在 Java 中更新 HashMap 中的一个值。本文介绍了如何在 Java 中使用 HashMap 类中包含的两个方法-put() 和 replace() 更新 HashMap 中的值。
Java 中的 hashmap 和 map 之间的区别
发布时间:2023/12/17 浏览次数:79 分类:Java
-
本文介绍了 Java 中的 hashmap 和 map 接口之间的区别。本教程介绍了 Java 中 Map 和 HashMap 之间的主要区别。在 Java 中,Map 是用于以键值对存储数据的接口,
在 Java 中获取用户主目录
发布时间:2023/12/17 浏览次数:218 分类:Java
-
这篇文章向你展示了如何在 Java 中获取用户主目录。本教程介绍了如何在 Java 中获取用户主目录,并列出了一些示例代码以指导你完成该主题。
Java 中 size 和 length 的区别
发布时间:2023/12/17 浏览次数:179 分类:Java
-
这篇文章教你如何知道 Java 中大小和长度之间的区别。本教程介绍了 Java 中大小和长度之间的区别。我们还列出了一些示例代码以帮助你理解该主题。
Java 中的互斥锁
发布时间:2023/12/17 浏览次数:111 分类:Java
-
了解有关 Java 中互斥锁的一切,在计算机科学领域,互斥或互斥被称为并发控制的属性。每台计算机都使用称为线程的最小程序指令序列。有一次,计算机在一个线程上工作。为了更好地理解,