English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Dans ce tutoriel, nous allons apprendre à utiliser des exemples Java FileWriter et ses méthodes.
La classe FileWriter du paquet java.io peut être utilisée pour écrire des données (en unités de caractères) dans un fichier.
Il hérite de la classe OutputStreamWriter.
Avant de comprendre davantage FileWriter, assurez-vous de comprendreJava file.
Pour créer un FileWriter, nous devons d'abord importer le paquet Java.io.FileWriter. Une fois le paquet importé, vous pouvez créer un FileWriter.
1.Utilisation du nom de fichier
FileWriter output = new FileWriter(String name);
Ici, nous avons créé un FileWriter qui se liera au nom de fichier spécifié.
2.Utilisation de l'objet fichier
FileWriter input = new FileWriter(File fileObj);
Ici, nous avons créé un FileWriter qui se liera au fichier spécifié par l'objet fichier.
Dans l'exemple ci-dessus, les données sont stockées en utilisant un certain encodage de caractères par défaut.
Mais, en raison de Java 11nous pouvons également spécifier le type d'encodage des caractères (UTF8ouUTF16)
FileWriter input = new FileWriter(String file, Charset cs);
Ici, nous utilisons la classe Charset pour spécifier l'encodage des caractères du FileWriter.
FileWriter类为Writer类中出现的不同方法提供了实现。
write() -向写入器写一个字符
write(char[] array) -将指定数组中的字符写入写入器
write(String data) -将指定的字符串写入写入器
import java.io.FileWriter; public class Main { public static void main(String args[]) { String data = "This is the data in the output file"; try { // 创建 FileWriter FileWriter output = new FileWriter("output.txt"); // 将字符串写入文件 output.write(data); //关闭 writer output.close(); } catch (Exception e) { e.getStackTrace(); } } }
在上面的示例中,我们创建了一个名为output的文件写入器。输出阅读器与output.txt文件链接。
FileWriter output = new FileWriter("output.txt");
要将数据写入文件,我们使用了write()方法。
在这里,当我们运行程序时,output.txt文件将填充以下内容。
This is a line of text inside the file.
getEncoding()方法可用于获取用于写入数据的编码类型。例如,
import java.io.FileWriter; import java.nio.charset.Charset; class Main { public static void main(String[] args) { String file = "output.txt"; try { //使用默认编码创建一个FileReader FileWriter output1 = new FileWriter(file); //创建一个FileReader指定编码 FileWriter output2 = new FileWriter(file, Charset.forName("UTF8"); //返回阅读器的字符编码 System.out.println("output1字符编码: " + output1.getEncoding()); System.out.println("output2字符编码: " + output2.getEncoding()); // Close reader output1.close(); output2.close(); } catch(Exception e) { e.getStackTrace(); } } }
Output result
output1character encoding: Cp1252 output2character encoding: UTF8
In the above example, we created2a file writer, named output1and output2.
output1No character encoding specified. Therefore, the getEncoding() method returns the default character encoding.
output2Specify character encodingUTF8. Therefore, the getEncoding() method returns the specified character encoding.
Note: We have used the Charset.forName() method to specify the type of character encoding.
To close the file writer, we can use the close() method. Once the close() method is called, the writer cannot be used to write data.
Method | Description |
---|---|
flush() | Force all existing data in the writer to be written to the corresponding destination |
append() | Insert the specified character into the current writer |