We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(24)
我喜欢 Apache commons utils 来完成此类任务,并在测试时广泛使用这个确切的用例(从类路径读取文件),特别是从
/src/test/resources
读取 JSON 文件作为单元的一部分/ 集成测试。例如,出于测试目的,最好捕获 IOException 并抛出 RuntimeException - 您的测试类可能如下所示
I like Apache commons utils for this type of stuff and use this exact use-case (reading files from classpath) extensively when testing, especially for reading JSON files from
/src/test/resources
as part of unit / integration testing. e.g.For testing purposes, it can be nice to catch the
IOException
and throw aRuntimeException
- your test class could look like e.g.如果您想要逐行返回值作为
List
,Guava 还具有Files.readLines()
:请参阅 此处 比较 3 种方式(
BufferedReader
与 Guava 的Files
与 Guava 的Resources
)从文本文件中获取String
。Guava also has
Files.readLines()
if you want a return value asList<String>
line-by-line:Please refer to here to compare 3 ways (
BufferedReader
vs. Guava'sFiles
vs. Guava'sResources
) to getString
from a text file.这是我的方法效果很好
Here is my approach worked fine
如果您包含 Guava,那么您可以使用:(
其他解决方案提到了 Guava 的其他方法,但它们已被弃用)
If you include Guava, then you can use:
(Other solutions mentioned other method for Guava but they are deprecated)
以下代码对我有用:
The following cods work for me:
我制作了这样的无依赖静态方法:
I made NO-dependency static method like this:
我自己也经常遇到这个问题。为了避免对小项目的依赖,我经常
当我不需要 commons io 等时,编写一个小实用函数。这是
将文件内容加载到字符串缓冲区中的代码:
在这种情况下指定编码很重要,因为您可能有
用UTF-8编辑你的文件,然后将其放入jar中,然后打开的计算机
文件可能将 CP-1251 作为其本机文件编码(例如);所以在
在这种情况下,您永远不知道目标编码,因此显式
编码信息至关重要。
此外,逐个字符读取文件的循环似乎效率低下,但它用于
BufferedReader,所以实际上相当快。
I often had this problem myself. To avoid dependencies on small projects, I often
write a small utility function when I don't need commons io or such. Here is
the code to load the content of the file in a string buffer :
Specifying the encoding is important in that case, because you might have
edited your file in UTF-8, and then put it in a jar, and the computer that opens
the file may have CP-1251 as its native file encoding (for example); so in
this case you never know the target encoding, therefore the explicit
encoding information is crucial.
Also the loop to read the file char by char seems inefficient, but it is used on a
BufferedReader, and so actually quite fast.
这是使用 Java 11 的
Files.readString
:Here's a solution using Java 11's
Files.readString
:如果您想从文件等项目资源中获取字符串
在项目的 src/main/resources 中的 testcase/foo.json 中,执行以下操作:
请注意,其他一些示例中缺少 getClassLoader() 方法。
If you want to get your String from a project resource like the file
testcase/foo.json in src/main/resources in your project, do this:
Note that the getClassLoader() method is missing on some of the other examples.
使用 Apache commons 的 FileUtils。它有一个方法 readFileToString
Use Apache commons's FileUtils. It has a method readFileToString
我使用以下内容从
类路径
读取资源文件:不需要第三方依赖项。
I'm using the following for reading resource files from the
classpath
:No third party dependencies required.
至少从 Apache commons-io 2.5 开始,IOUtils.toString() 方法支持 URI 参数并返回位于类路径上的 jar 内的文件内容:
At least as of Apache commons-io 2.5, the IOUtils.toString() method supports an URI argument and returns contents of files located inside jars on the classpath:
通过一组静态导入,Guava 解决方案可以是非常紧凑的单行:
需要以下导入:
With set of static imports, Guava solution can be very compact one-liner:
The following imports are required:
yegor256 找到了一个不错的解决方案使用 Apache Commons IO:
yegor256 has found a nice solution using Apache Commons IO:
Guava 有一个“toString”方法,用于将文件读入字符串:
此方法不需要文件位于类路径中(如 Jon Skeet 之前的答案)。
Guava has a "toString" method for reading a file into a String:
This method does not require the file to be in the classpath (as in Jon Skeet previous answer).
apache-commons-io 有一个实用程序名称
FileUtils
:apache-commons-io has a utility name
FileUtils
:我喜欢阿科西基关于愚蠢的扫描仪技巧的回答。这是我看到的最简单的方法,不需要外部依赖,可以在 Java 8 中工作(实际上可以追溯到 Java 5)。这里有一个更简单的答案如果您可以使用 Java 9 或更高版本(因为
InputStream.readAllBytes()
是在 Java 9 中添加的):如果您担心文件名是错误和/或关于关闭流,您可以稍微扩展一下:
I like akosicki's answer with the Stupid Scanner Trick. It's the simplest I see without external dependencies that works in Java 8 (and in fact all the way back to Java 5). Here's an even simpler answer if you can use Java 9 or higher (since
InputStream.readAllBytes()
was added at Java 9):If you're concerned about the filename being wrong and/or about closing the stream, you can expand this a little:
您可以使用以下 Java 代码
You can use the following code form Java
是的,Guava 在 Guava 中提供了此功能。 github.io/guava/releases/snapshot/api/docs/com/google/common/io/Resources.html" rel="noreferrer">
资源
类。例如:Yes, Guava provides this in the
Resources
class. For example:您可以使用旧的 愚蠢的扫描仪技巧 oneliner 来完成此操作,而无需任何额外的依赖,如番石榴:
伙计们,不要使用第 3 方的东西,除非你真的需要它。 JDK 中已经有很多功能。
You can use the old Stupid Scanner trick oneliner to do that without any additional dependency like guava:
Guys, don't use 3rd party stuff unless you really need that. There is a lot of functionality in the JDK already.
纯粹、简单、jar 友好的 Java 8+ 解决方案
如果您使用 Java 8 或更高版本,下面这个简单的方法就可以很好地工作:
而且它还适用于 jar 文件中的资源。
关于文本编码:如果您未指定,
InputStreamReader
将使用默认的系统字符集。您可能需要自己指定它以避免解码问题,如下所示:避免不必要的依赖项
始终不喜欢依赖于大而胖的库。除非您已经在使用 Guava 或 Apache Commons IO 来执行其他任务,否则将这些库添加到您的项目中只是为了能够从文件中读取数据似乎有点太多了。
Pure and simple, jar-friendly, Java 8+ solution
This simple method below will do just fine if you're using Java 8 or greater:
And it also works with resources in jar files.
About text encoding:
InputStreamReader
will use the default system charset in case you don't specify one. You may want to specify it yourself to avoid decoding problems, like this:Avoid unnecessary dependencies
Always prefer not depending on big, fat libraries. Unless you are already using Guava or Apache Commons IO for other tasks, adding those libraries to your project just to be able to read from a file seems a bit too much.
对于 Java 7:
对于 Java 11:
For java 7:
For Java 11: