📅  最后修改于: 2020-09-27 04:47:13             🧑  作者: Mango
Java FileWriter类用于将面向字符的数据写入文件。它是面向字符的类,用于Java中的文件处理。
与FileOutputStream类不同,您不需要将字符串转换为字节数组,因为它提供了直接写入字符串的方法。
让我们看一下Java.io.FileWriter类的声明:
public class FileWriter extends OutputStreamWriter
Constructor | Description |
---|---|
FileWriter(String file) | Creates a new file. It gets file name in string. |
FileWriter(File file) | Creates a new file. It gets file name in File object. |
Method | Description |
---|---|
void write(String text) | It is used to write the string into FileWriter. |
void write(char c) | It is used to write the char into FileWriter. |
void write(char[] c) | It is used to write char array into FileWriter. |
void flush() | It is used to flushes the data of FileWriter. |
void close() | It is used to close the FileWriter. |
在此示例中,我们使用Java FileWriter类将数据写入文件testout.txt中。
package com.javatpoint;
import java.io.FileWriter;
public class FileWriterExample {
public static void main(String args[]){
try{
FileWriter fw=new FileWriter("D:\\testout.txt");
fw.write("Welcome to javaTpoint.");
fw.close();
}catch(Exception e){System.out.println(e);}
System.out.println("Success...");
}
}
输出:
Success...
testout.txt:
Welcome to javaTpoint.