将文件读入String最简单的方法是什么? [英] What is simplest way to read a file into String?
问题描述
我正在尝试将一个简单的文本文件读入一个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(content);
这使用 java.util.Scanner
,告诉它用 \Z
,这是字符串锚的结尾。这最终使输入有一个实际的令牌,这是整个文件,所以可以通过一次调用 next()
来读取。
有一个构造函数,它需要一个文件
和一个 String charSetName
(其他许多重载)。这两个构造函数可能会抛出 FileNotFoundException
,但是像所有 Scanner
方法一样,没有 IOException
可以抛出这些构造函数。
您可以通过 Scanner =http://download.oracle.com/javase/6/docs/api/java/util/Scanner.html#ioException%28%29 =noreferrer> 为了完整起见,如果您拥有这些非常有信誉和非常有用的第三方库,那么这些是一些非常好的选择: 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 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 This uses a There is a constructor that takes a You can query the For completeness, these are some really good options if you have these very reputable and highly useful third party libraries: 这篇关于将文件读入String最简单的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! ioException()
方法,如果 IOException
发生或不发生。您可能还需要明确 close()
您阅读内容后的 Scanner
,因此也许存储 $>
另请参阅
相关问题
第三方库选项
Guava
com.google.common.io.Files
包含许多有用的方法。相关的是:
String toString(File, Charset)
String
列表与LT;字符串> readLines(File,Charset)
列表< String>
,每行一个条目
Apache Commons / IO
org.apache.commons.io.IOUtils
也提供类似的功能:
String toString(InputStream,String encoding)
InputStream
的内容作为 String
列表readLines(InputStream,String encoding)
列表
String
,每行一个条目
Relat ed问题
String fileContents = XXX.readFile(myFile/*File*/)
.. rather anything that looks as simple as this?IOException
handling you wouldn't want to).String content = new Scanner(new File("filename")).useDelimiter("\\Z").next();
System.out.println(content);
java.util.Scanner
, telling it to delimit the input with \Z
, 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()
.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.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
Third-party library options
Guava
com.google.common.io.Files
contains many useful methods. The pertinent ones here are:
String toString(File, Charset)
String
List<String> readLines(File, Charset)
List<String>
, one entry per lineApache Commons/IO
org.apache.commons.io.IOUtils
also offer similar functionality:
String toString(InputStream, String encoding)
InputStream
as a String
List readLines(InputStream, String encoding)
List
of String
, one entry per lineRelated questions