java 在 FileReader 和 InputStreamReader 之间进行选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9938520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:58:28  来源:igfitidea点击:

Choosing between FileReader and InputStreamReader

javajava-io

提问by Basimalla Sebastin

I have two methods to read Text File In java one using FileReader and Other File InputStream

我有两种方法可以在 java 中读取文本文件,一种是使用 FileReader 和其他文件 InputStream

FileReader fr=new FileReader("C:\testq\test.txt");
BufferedReader br=new BufferedReader(fr);
String s;
while((s=br.readLine())!=null){
    System.out.println("value are "+s);
}

and Other is

和其他是

FileInputStream fstream = new FileInputStream("C:\testnew\out.text");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null){
   System.out.println (strLine);
}

Though both give me output ...I just want to know which is the best way to do it.

虽然两者都给了我输出......我只是想知道哪种方法是最好的。

回答by Jon Skeet

I would stronglyadvise using InputStreamReaderinstead of FileReader, but explicitly specifying the character encoding. That's really the biggest benefit of using InputStreamReader(and the lack of ability to specify an encoding for FileReaderis a major hole in the API, IMO).

强烈建议使用InputStreamReader而不是FileReader,但明确指定字符编码。这确实是使用的最大好处InputStreamReader(并且缺乏指定编码的能力FileReader是 API 中的一个主要漏洞,IMO)。

I'd also remove the "layer" using DataInputStream- just pass the FileInputStreamto the InputStreamReaderconstructor.

我还将使用删除“层” DataInputStream- 只需将 传递FileInputStreamInputStreamReader构造函数。

Before Java 8

在 Java 8 之前

Alternatively, consider using some of the many convenience methods in Guavawhich can make this sort of thing muchsimpler. For example:

或者,考虑使用Guava 中许多方便的方法中的一些,这可以使这种事情变得更简单。例如:

File file = new File("C:\testnew\out.text");
List<String> lines = Files.readLines(file, Charsets.UTF_8));

From Java 8

从 Java 8

Java 8 introduced a bunch of new classes and methods in java.nio.files, many of which default (sensibly) to UTF-8:

Java 8 在 中引入了一堆新的类和方法java.nio.files,其中许多默认(明智地)为 UTF-8:

Path path = Paths.get("C:\testnew\out.text");
List<String> lines = Files.readAllLines(path);

回答by Guillaume Polet

Both approaches are ok because you use a BufferedReader which highly improves performance over a no-buffer approach. In your second case, there is not need to wrap the FileInputStream in a DataInputStream. The last approach, let's you specify the file encoding through the InputStreamReader which is usually an important thing.

这两种方法都可以,因为您使用了 BufferedReader,它比无缓冲区方法大大提高了性能。在第二种情况下,不需要将 FileInputStream 包装在 DataInputStream 中。最后一种方法,让我们通过 InputStreamReader 指定文件编码,这通常是一件很重要的事情。

回答by Chandra Sekhar

It depends, if you want to read a file which just contains text (i mean a text file) then you should use first case.

这取决于,如果你想读取一个只包含文本的文件(我的意思是一个文本文件),那么你应该使用第一种情况。

If you want to read some file which represents binary data (i mean image file or video file etc), you should use the second case.

如果你想读取一些代表二进制数据的文件(我的意思是图像文件或视频文件等),你应该使用第二种情况。

回答by Alex Stybaev

Well, you create a BufferedReaderfrom the FileInputStreamoriginally. You should do it as following:

好吧,您BufferedReaderFileInputStream最初创建了一个。你应该这样做:

FileInputStream fstream = new FileInputStream("C:\testnew\out.text");
BufferedInputStream bstream = new BufferedInputStream(fstream);

To get a proper stream-related approach.

获得正确的流相关方法。