将文件读入String最简单的方法是什么? [英] What is simplest way to read a file into String?

查看:114
本文介绍了将文件读入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> ioException() 方法,如果 IOException 发生或不发生。您可能还需要明确 close() 您阅读内容后的 Scanner ,因此也许存储

另请参阅





相关问题








第三方库选项



为了完整起见,如果您拥有这些非常有信誉和非常有用的第三方库,那么这些是一些非常好的选择:



Guava



com.google.common.io.Files 包含许多有用的方法。相关的是:





Apache Commons / IO



org.apache.commons.io.IOUtils 也提供类似的功能:




  • String toString(InputStream,String encoding)


    • 使用指定的字符编码将 InputStream 的内容作为 String


  • 列表readLines(InputStream,String encoding)


    • 作为一个(原始的)列表 String ,每行一个条目




      Relat ed问题




      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 \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().

      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


      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:

      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 a String
      • List readLines(InputStream, String encoding)
        • ... as a (raw) List of String, one entry per line

      Related questions

      这篇关于将文件读入String最简单的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆