为什么在 java 7 中没有 Files.readAllLines(String path)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12705111/
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
Why is threre not a Files.readAllLines(String path) in java 7?
提问by Chirlo
I'm tryting to learn the nio 2
package in Java 7 and i stumbled upon the Files.readAllLines(Path p, Charset cs)
method. I find it very useful, but i'm of the opinion that there should be a version without the cs
parameter, just like :
我正在尝试学习nio 2
Java 7 中的包,我偶然发现了该Files.readAllLines(Path p, Charset cs)
方法。我觉得它非常有用,但我认为应该有一个没有cs
参数的版本,就像:
public static List<String> readAllLines(String path)
throws IOException
{ return readAllLines(Paths.get(path), Charset.defaultCharset());}
I convinced that most of the time the method will be called with the default Charset anyway, so why no the shorcut. Is there anything i'm missing about charsets that would justify not having this method? I'm quite surprised because Scala has this option:
我确信在大多数情况下,无论如何都会使用默认字符集调用该方法,那么为什么不使用快捷方式。关于字符集,我有什么遗漏可以证明没有这种方法吗?我很惊讶,因为 Scala 有这个选项:
Source.fromFile("fileName").getLines
so i don't see why Java shouldn't. Any views?
所以我不明白为什么 Java 不应该。有什么意见吗?
回答by Tomasz Nurkiewicz
[...] most of the time the method will be called with the default Charset anyway,
[...] 大多数情况下,无论如何都会使用默认字符集调用该方法,
Not really. Most of the time it will be called with the charset that you expect the file to be encoded in. Typically these days it is UTF-8:
并不真地。大多数情况下,它将使用您希望文件编码的字符集调用它。现在通常是 UTF-8:
Files.readAllLines("fileName", StandardCharsets.UTF_8)
Your application can be executed on several platforms and operating systems, using different default character encoding. You don't want your application to break just because of that.
您的应用程序可以在多个平台和操作系统上执行,使用不同的默认字符编码。您不希望您的应用程序因此而中断。
I think it's a good choice, fixing wrong desing decisions from the past. Many old Java methods use default system encoding, causing inconsistent behaviour or application e.g. between Windows and Linux. Forcing to choose the character encoding simply makes your application more portable and safer.
我认为这是一个不错的选择,修复过去错误的设计决策。许多旧的 Java 方法使用默认系统编码,导致行为或应用程序不一致,例如在 Windows 和 Linux 之间。强制选择字符编码只会使您的应用程序更可移植和更安全。
BTW since you are mentioning io.Source
class - note that it returns an iterator instead of a List<String>
as Files
class does. The advantage: file is loaded lazily, not all at once to huge ArrayList<String>
. Disadvantage: you must close the source manually (which you can't do in your code snippet).
顺便说一句,因为你提的io.Source
类-请注意,它返回一个迭代器,而不是List<String>
作为Files
类一样。优点:文件加载延迟,而不是一下子加载到巨大的ArrayList<String>
. 缺点:您必须手动关闭源代码(您不能在代码片段中这样做)。
回答by user207421
You would have to ask the designers, but very probably they share my view that reading entire files into memory is not something to be encouraged. It does not scale, and it introduces unnecessary time and space costs. Process the file a line at a time.
您将不得不询问设计人员,但很可能他们同意我的观点,即不鼓励将整个文件读入内存。它不能扩展,并且会引入不必要的时间和空间成本。一次处理一行文件。