Java 简明教程
Java - FileReader Class
Introduction
Java.io.FileReader 类是一个用于读取字符文件的便捷类。以下是有关 FileReader 的重要说明 −
The Java.io.FileReader class is a convenience class for reading character files.Following are the important points about FileReader −
-
The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate.
-
FileReader is meant for reading streams of characters. For reading streams of raw bytes, use FileInputStream.
Class declaration
以下是 Java.io.FileReader 类声明 −
Following is the declaration for Java.io.FileReader class −
public class FileReader
extends InputStreamReader
Field
以下是 Java.io.FileReader 类的字段 −
Following are the fields for Java.io.FileReader class −
-
protected Object lock − This is the object used to synchronize operations on this stream.
Class constructors
Sr.No. |
Constructor & Description |
1 |
FileReader(File file) This constructor creates a new FileReader, given the File to read from. |
2 |
FileReader(FileDescriptor fd) This constructor creates a new FileReader, given the FileDescriptor to read from. |
3 |
FileReader(String fileName) This constructor creates a new FileReader, given the name of the file to read from. |
一旦你有了 FileReader 对象,就可以使用一系列帮助方法来操作文件。
Once you have FileReader 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 int read() throws IOException Reads a single character. Returns an int, which represents the character read. |
2 |
public int read(char [] c, int offset, int len) Reads characters into an array. Returns the number of characters read. |
Example 1
以下示例展示了 Java FileReader 类的用法。我们已创建一个名为 Hello1.txt 的文件引用,以在当前目录中创建。然后,我们使用 createNewFile() 创建一个新文件。现在,可以通过传递先前创建的文件引用来创建一个 FileWriter 对象,并将一些内容写入文件。使用 FileReader() 类,我们正在读取该文件并打印其内容。
The following example shows the usage of Java FileReader 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 FileReader 类的用法。我们已创建一个名为 Hello1.txt 的文件引用,以在提供的目录中创建。然后,我们使用 createNewFile() 创建一个新文件。现在,可以通过传递先前创建的文件引用来创建一个 FileWriter 对象,并将一些内容写入文件。使用 FileReader() 类,我们正在读取该文件并打印其内容。
The following example shows the usage of Java FileReader 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();
}
}