Java FileReader (Java FileReader)
- Java FileReader class is part of java.io package. Java FileReader类是java.io软件包的一部分。
- The FileReader is a subclass of
InputStreamReader
class. FileReader是InputStreamReader
类的子类。 - Java FileReader is recommended for reading text data from a file compared to
FileInputStream
. 与FileInputStream
相比,建议使用Java FileReader从文件读取文本数据。 - FileReader is meant for reading streams of characters. So it’s a good choice to read String based data. FileReader用于读取字符流。 因此,读取基于字符串的数据是一个不错的选择。
- The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. 此类的构造函数假定默认字符编码和默认字节缓冲区大小是适当的。
- FileReaders are usually wrapped by higher-level objects such as
BufferedReader
, which improve performance and provide more convenient ways to work with the data. FileReader通常由更高级的对象(如BufferedReader
包装,这些对象可以提高性能并提供更方便的数据处理方式。
FileReader类层次结构 (FileReader Class Hierarchy)
FileReader构造函数 (FileReader Constructors)
Let’s have a quick look at FileReader constructors.
让我们快速看一下FileReader 构造函数 。
FileReader(File file)
: Creates a new FileReader object using specified file object to read from. It will throwFileNotFoundException
if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.FileReader(File file)
:使用要读取的指定文件对象创建一个新的FileReader对象。 如果该文件不存在,不是目录而是常规文件,或者由于某些其他原因而无法打开进行读取,则会抛出FileNotFoundException
。FileReader(FileDescriptor fd)
: Creates a new FileReader object using specifiedFileDescriptor
object to read from.FileReader(FileDescriptor fd)
:使用要读取的指定FileDescriptor
对象创建一个新的FileReader对象。FileReader(String fileName)
: Creates a new FileReader object using specified name of the file to read from. It will throwFileNotFoundException
if the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.FileReader(String fileName)
:使用要读取的文件的指定名称创建一个新的FileReader对象。 如果指定的文件不存在,不是目录而是常规文件,或者由于某些其他原因而无法打开进行读取,则会抛出FileNotFoundException
。
Java FileReader示例 (Java FileReader Example)
Let’s have a look at the below methods and example programs of FileReader class.
让我们看一下FileReader类的以下方法和示例程序。
读() (read())
This method reads a single character. This method will block until a character is available, an I/O error occurs, or the end of the stream is reached. It returns the character read as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached.
此方法读取单个字符。 该方法将阻塞,直到有字符可用,发生I / O错误或到达流的末尾为止。 它返回读取的字符,范围为0到65535(0x00-0xffff)之间的整数,如果已到达流的末尾,则返回-1。
Let’s have look at the below example program.
让我们看下面的示例程序。
package com.journaldev.examples;
import java.io.File;
import java.io.FileReader;
/**
* Java Read file FileReader
*
* @author pankaj
*
*/
public class FileReaderReadExample {
public static void main(String[] args) {
File file = null;
FileReader reader = null;
try {
file = new File("D:/data/file.txt");
reader = new FileReader(file);
int i;
while ((i=reader.read())!= -1) {
System.out.print((char)i);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (reader != null) {
reader.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
The output of the above program is below:
上面程序的输出如下:
Hello World.
This is a FileReader Example.
FileReader implements AutoCloseable
interface, hence we can user try with resource while using FileReader class. Let’s have look at the below example program.
FileReader实现了AutoCloseable
接口 ,因此我们可以在使用FileReader类时尝试使用资源 。 让我们看下面的示例程序。
package com.journaldev.examples;
import java.io.File;
import java.io.FileReader;
/**
* Java Read file FileReader using try with resource
*
* @author pankaj
*
*/
public class FileReaderReadUsingTryWithResource {
public static void main(String[] args) {
File file = new File("D:/data/file.txt");
try (FileReader reader = new FileReader(file);){
int i;
while ((i=reader.read())!= -1) {
System.out.print((char)i);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output of the above program is below:
上面程序的输出如下:
Hello World.
This is a FileReader Example.
读(char [] cbuf) (read(char[] cbuf))
This method reads characters into an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached. It returns the number of characters read, or -1 if the end of the stream has been reached. Let’s have look at the below example program.
此方法将字符读入数组。 该方法将阻塞,直到某些输入可用,发生I / O错误或到达流的末尾为止。 它返回读取的字符数,如果已到达流的末尾,则返回-1。 让我们看下面的示例程序。
package com.journaldev.examples;
import java.io.File;
import java.io.FileReader;
/**
* Java Read file FileReader using read(char[] cbuf) method
*
* @author pankaj
*
*/
public class ReadFileUsingFileReader {
public static void main(String[] args) {
File file = new File("D:/data/file.txt");
try (FileReader reader = new FileReader(file);){
char[] cs = new char[100];
reader.read(cs);
for (char c : cs) {
System.out.print(c);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
The output of the above program is below:
上面程序的输出如下:
Hello World.
This is a FileReader Example.
read(char [] cbuf,int关闭,int len) (read(char[] cbuf, int off, int len))
This method reads characters into a portion of an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached. It returns the number of characters read, or -1 if the end of the stream has been reached.
此方法将字符读入数组的一部分。 该方法将阻塞,直到某些输入可用,发生I / O错误或到达流的末尾为止。 它返回读取的字符数,如果已到达流的末尾,则返回-1。
Parameters:
参数 :
- cbuf : Destination buffer cbuf:目标缓冲区
- off : Offset at which to start storing characters off:开始存储字符的偏移量
- len : Maximum number of characters to read len:要读取的最大字符数
package com.journaldev.examples;
import java.io.File;
import java.io.FileReader;
/**
* Java Read file FileReader using read(char[] cbuf, int off, int len) method
*
* @author pankaj
*
*/
public class ReadFileUsingFileReaderExample {
public static void main(String[] args) {
File file = new File("D:/data/file.txt");
try (FileReader reader = new FileReader(file);){
char[] cs = new char[100];
reader.read(cs, 0, 11);
for (char c : cs) {
System.out.print(c);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
//Output: Hello World
读取(CharBuffer目标) (read(CharBuffer target))
This method reads characters into the specified character buffer. The buffer is used as a repository of characters as-is: the only changes made are the results of a put operation. No flipping or rewinding of the buffer is performed. It returns the number of characters added to the buffer, or -1 if this source of characters is at its end.
此方法将字符读入指定的字符缓冲区。 缓冲区按原样用作字符存储库:所做的唯一更改是放置操作的结果。 不执行缓冲区的翻转或倒带。 它返回添加到缓冲区的字符数;如果此字符源在末尾,则返回-1。
package com.journaldev.examples;
import java.io.File;
import java.io.FileReader;
import java.nio.CharBuffer;
/**
* Java Read file FileReader using CharBuffer
*
* @author pankaj
*
*/
public class ReadFileUsingFileReaderCharBuffer {
public static void main(String[] args) {
File file = new File("D:/data/file.txt");
try (FileReader reader = new FileReader(file);){
//create char buffer with the capacity of 50
CharBuffer cs = CharBuffer.allocate(50);
//read characters into char buffer
reader.read(cs);
//flip char buffer
cs.flip();
System.out.println(cs.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
跳过(长n) (skip(long n))
This method skips the n number of character and returns the number of skipped characters.
此方法跳过n个字符,并返回跳过的字符数。
package com.journaldev.examples;
import java.io.File;
import java.io.FileReader;
/**
* Java Read file FileReader using skip method
*
* @author pankaj
*
*/
public class FileReaderSkipExample {
public static void main(String[] args) {
File file = null;
FileReader reader = null;
try {
file = new File("D:/data/file.txt");
reader = new FileReader(file);
//skip first 6 characters
reader.skip(6);
int i;
while ((i=reader.read())!= -1) {
System.out.print((char)i);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (reader != null) {
reader.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
Output:
输出:
World.
This is a FileReader Example.
Also check java read text file for more about how to read text file in java.
还要检查Java读取文本文件,以获取有关如何在Java中读取文本文件的更多信息。
Java FileReader与FileInputStream (Java FileReader vs FileInputStream)
- FileReader is used for reading streams of character while FileInputStream is used for reading streams of bytes like raw image data. FileReader用于读取字符流,而FileInputStream用于读取字节流(如原始图像数据)。
- FileReader is good for reading text file like java source code file while FileInputStream is good for reading binary files like .class files. FileReader适合读取Java源代码文件之类的文本文件,而FileInputStream适合读取.class文件之类的二进制文件。
That’s all for Java FileReader, I hope nothing important got missed here.
这就是Java FileReader的全部内容,我希望这里没有重要的事情。
Reference: API Doc
参考: API文档