Java 简明教程
Java - FileWriter Class
Introduction
Java FileWriter 类是用于编写字符文件的便捷类。下面是有关 FileWriter 的重要事项−
The Java FileWriter class is a convenience class for writing character files.Following are the important points about FileWriter −
-
The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable.
-
FileWriter is meant for writing streams of characters. For writing streams of raw bytes, use FileOutputStream.
Class declaration
以下是 Java.io.FileWriter 类的声明 −
Following is the declaration for Java.io.FileWriter class −
public class FileWriter
extends OutputStreamWriter
Field
以下是 Java.io.FileWriter 类的字段 −
Following are the fields for Java.io.FileWriter class −
-
protected Object lock − This is the object used to synchronize operations on this stream.
Class constructors
Sr.No. |
Constructor & Description |
1 |
FileWriter(File file) This constructor creates a FileWriter object given a File object. |
2 |
FileWriter(File file, boolean append) This constructor creates a FileWriter object given a File object with a boolean indicating whether or not to append the data written. |
3 |
FileWriter(FileDescriptor fd) This constructor creates a FileWriter object associated with the given file descriptor. |
4 |
FileWriter(String fileName) This constructor creates a FileWriter object, given a file name. |
5 |
FileWriter(String fileName, boolean append) This constructor creates a FileWriter object given a file name with a boolean indicating whether or not to append the data written. |
一旦你拥有 FileWriter 对象,就有一个帮助器方法列表,可用于操作文件。
Once you have FileWriter object in hand, then there is a list of helper methods, which can be used to manipulate the files.
Sr.No. |
Method & Description |
1 |
public void write(int c) throws IOException Writes a single character. |
2 |
public void write(char [] c, int offset, int len) Writes a portion of an array of characters starting from offset and with a length of len. |
3 |
public void write(String s, int offset, int len) Write a portion of a String starting from offset and with a length of len. |
Example 1
以下示例展示了 Java FileWriter 类的用法。我们创建了名称为 Hello1.txt 的文件引用,要在当前目录中创建它。然后我们使用 createNewFile() 创建一个新文件。现在,通过传递前面创建的文件引用创建了一个 FileWriter 对象,并将一些内容写入该文件。使用 FileReader() 类,我们正在读取该文件,并打印其内容。
The following example shows the usage of Java FileWriter class. We’ve created a File reference with name Hello1.txt to be created in current directory. Then we’re creating a new file using createNewFile(). Now a FileWriter object is created by passing the file reference created earlier and some content is written to the file. Using FileReader() class, we’re reading that file and its contents are printed.
package com.tutorialspoint;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileDemo {
public static void main(String args[])throws IOException {
File file = new File("Hello1.txt");
// creates the file
file.createNewFile();
// creates a FileWriter Object
FileWriter writer = new FileWriter(file);
// Writes the content to the file
writer.write("This\n is\n an\n example\n");
writer.flush();
writer.close();
// Creates a FileReader Object
FileReader fr = new FileReader(file);
char [] a = new char[50];
fr.read(a); // reads the content to the array
for(char c : a)
System.out.print(c); // prints the characters one by one
fr.close();
}
}
Example 2
以下示例展示了 Java FileWriter 类的用法。我们创建了名称为 Hello1.txt 的文件引用,要在提供的目录中创建它。然后我们使用 createNewFile() 创建一个新文件。现在,通过传递前面创建的文件引用创建了一个 FileWriter 对象,并将一些内容写入该文件。使用 FileReader() 类,我们正在读取该文件,并打印其内容。
The following example shows the usage of Java FileWriter class. We’ve created a File reference with name Hello1.txt to be created in a provided directory. Then we’re creating a new file using createNewFile(). Now a FileWriter object is created by passing the file reference created earlier and some content is written to the file. Using FileReader() class, we’re reading that file and its contents are printed.
package com.tutorialspoint;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileDemo {
public static void main(String args[])throws IOException {
File file = new File("F:/Test2/Hello1.txt");
// creates the file
file.createNewFile();
// creates a FileWriter Object
FileWriter writer = new FileWriter(file);
// Writes the content to the file
writer.write("This\n is\n an\n example\n");
writer.flush();
writer.close();
// Creates a FileReader Object
FileReader fr = new FileReader(file);
char [] a = new char[50];
fr.read(a); // reads the content to the array
for(char c : a)
System.out.print(c); // prints the characters one by one
fr.close();
}
}