将文件读入字符串的最简单方法是什么? [英] What is simplest way to read a file into String?
问题描述
我正在尝试将一个简单的文本文件读入一个字符串.当然,有获取输入流并使用 readLine() 进行迭代并将内容读入 String 的常用方法.
过去已经这样做了数百次,我只是想知道如何以最少的代码行做到这一点?java 中不是有类似 String fileContents = XXX.readFile(myFile/*File*/)
.. 的东西吗?
我知道有像 Apache Commons IO 这样的库提供了这样的简化,甚至我可以编写一个简单的 Util 类来做到这一点.但我想知道的是 - 这是每个人都需要的如此频繁的操作,那么为什么 Java 不提供如此简单的功能呢?难道真的没有一种方法可以将文件读入具有某种默认或指定编码的字符串吗?
是的,您可以在一行中完成此操作(尽管对于强大的 IOException
处理,您不希望这样做).
String content = new Scanner(new File("filename")).useDelimiter("\Z").next();System.out.println(内容);
这使用 java.util.扫描器
,告诉它用分隔输入,这是字符串锚点的结尾.这最终使输入具有一个实际标记,即整个文件,因此可以通过调用一次
next()
来读取它.
有 一个构造函数,它接受一个 File
和一个 String charSetName
(以及许多其他重载).这两个构造函数可能会抛出FileNotFoundException
,但与所有Scanner
方法一样,除了这些构造函数之外,不会抛出任何IOException
.
您可以通过 ioException()
方法是否发生 IOException
.您可能还想明确close()
读取内容后的 Scanner
,因此最好将 Scanner
引用存储在局部变量中.>
另见
相关问题
- 使用 java.util.Scanner 验证输入 - 有很多例子更典型的用法
第三方库选项
为了完整起见,如果您拥有这些信誉良好且非常有用的第三方库,这些是一些非常好的选择:
番石榴
com.google.common.io.Files
包含许多有用的方法.这里的相关内容是:
String toString(File, Charset)
- 使用给定的字符集,将文件中的所有字符读入
String
- 使用给定的字符集,将文件中的所有字符读入
List
readLines(File, Charset) - ... 将文件中的所有行读入
List
,每行一个条目
- ... 将文件中的所有行读入
Apache Commons/IO
org.apache.commons.io.IOUtils
也提供类似的功能:
String toString(InputStream, String encoding)
- 使用指定的字符编码,将
InputStream
的内容作为String
- 使用指定的字符编码,将
List readLines(InputStream, String encoding)
- ... 作为
String
的(原始)List
,每行一个条目
- ... 作为
相关问题
I am trying to read a simple text file into a String. Of course there is the usual way of getting the input stream and iterating with readLine() and reading contents into String.
Having done this hundreds of times in past, I just wondered how can I do this in minimum lines of code? Isn't there something in java like String fileContents = XXX.readFile(myFile/*File*/)
.. rather anything that looks as simple as this?
I know there are libraries like Apache Commons IO which provide such simplifications or even I can write a simple Util class to do this. But all that I wonder is - this is a so frequent operation that everyone needs then why doesn't Java provide such simple function? Isn't there really a single method somewhere to read a file into string with some default or specified encoding?
Yes, you can do this in one line (though for robust IOException
handling you wouldn't want to).
String content = new Scanner(new File("filename")).useDelimiter("\Z").next();
System.out.println(content);
This uses a java.util.Scanner
, telling it to delimit the input with , which is the end of the string anchor. This ultimately makes the input have one actual token, which is the entire file, so it can be read with one call to
next()
.
There is a constructor that takes a File
and a String charSetName
(among many other overloads). These two constructor may throw FileNotFoundException
, but like all Scanner
methods, no IOException
can be thrown beyond these constructors.
You can query the Scanner
itself through the ioException()
method if an IOException
occurred or not. You may also want to explicitly close()
the Scanner
after you read the content, so perhaps storing the Scanner
reference in a local variable is best.
See also
Related questions
- Validating input using java.util.Scanner - has many examples of more typical usage
Third-party library options
For completeness, these are some really good options if you have these very reputable and highly useful third party libraries:
Guava
com.google.common.io.Files
contains many useful methods. The pertinent ones here are:
String toString(File, Charset)
- Using the given character set, reads all characters from a file into a
String
- Using the given character set, reads all characters from a file into a
List<String> readLines(File, Charset)
- ... reads all of the lines from a file into a
List<String>
, one entry per line
- ... reads all of the lines from a file into a
Apache Commons/IO
org.apache.commons.io.IOUtils
also offer similar functionality:
String toString(InputStream, String encoding)
- Using the specified character encoding, gets the contents of an
InputStream
as aString
- Using the specified character encoding, gets the contents of an
List readLines(InputStream, String encoding)
- ... as a (raw)
List
ofString
, one entry per line
- ... as a (raw)
Related questions
这篇关于将文件读入字符串的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!