Jon*_*eet 290
是的,Guava在Resources课堂上提供了这个.例如:
URL url = Resources.getResource("foo.txt");
String text = Resources.toString(url, Charsets.UTF_8);
Run Code Online (Sandbox Code Playgroud)
小智 162
您可以使用旧的Stupid Scanner技巧 oneliner来执行此操作,而无需像guava那样的任何其他依赖:
String text = new Scanner(AppropriateClass.class.getResourceAsStream("foo.txt"), "UTF-8").useDelimiter("\\A").next();
Run Code Online (Sandbox Code Playgroud)
伙计们,除非你真的需要,否则不要使用第三方的东西.JDK中已经有很多功能.
Kov*_*ryi 82
对于java 7:
new String(Files.readAllBytes(Paths.get(getClass().getResource("foo.txt").toURI())));
Run Code Online (Sandbox Code Playgroud)
Luc*_*sio 57
Guava有一个"toString"方法,用于将文件读入String:
import com.google.common.base.Charsets;
import com.google.common.io.Files;
String content = Files.toString(new File("/home/x1/text.log"), Charsets.UTF_8);
Run Code Online (Sandbox Code Playgroud)
此方法不要求文件位于类路径中(如Jon Skeet上一个答案).
Luc*_*iva 54
如果您使用的是Java 8,下面这个简单的方法就可以了:
/**
* Reads given resource file as a string.
*
* @param fileName path to the resource file
* @return the file's contents
* @throws IOException if read fails for any reason
*/
static String getResourceFileAsString(String fileName) throws IOException {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
try (InputStream is = classLoader.getResourceAsStream(fileName)) {
if (is == null) return null;
try (InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr)) {
return reader.lines().collect(Collectors.joining(System.lineSeparator()));
}
}
}
Run Code Online (Sandbox Code Playgroud)
它也适用于jar文件中的资源.
没有大型肥胖库.除非您已经在使用Guava或Apache Commons IO,否则将这些库添加到项目中只是为了能够将文件作为字符串读取似乎有点太多了.
由于这通常是一个实用程序函数,因此可能希望可以从静态实用程序类访问它.我稍微更改了实现,因此可以将方法声明为static.它的工作原理是一样的.这里是:
new InputStreamReader(isr, StandardCharsets.UTF_8);
Run Code Online (Sandbox Code Playgroud)
Ste*_*lis 41
yegor256 使用Apache Commons IO找到了一个很好的解决方案:
import org.apache.commons.io.IOUtils;
String text = IOUtils.toString(this.getClass().getResourceAsStream("foo.xml"),
"UTF-8");
Run Code Online (Sandbox Code Playgroud)
And*_*s_D 37
apache-commons-io有一个实用程序名称FileUtils:
URL url = Resources.getResource("myFile.txt");
File myFile = new File(url.toURI());
String content = FileUtils.readFileToString(myFile, "UTF-8"); // or any other encoding
Run Code Online (Sandbox Code Playgroud)
小智 16
我自己经常遇到这个问题.为了避免对小项目的依赖,我经常在不需要commons io等时编写一个小实用程序函数.以下是在字符串缓冲区中加载文件内容的代码:
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("path/to/textfile.txt"), "UTF-8"));
for (int c = br.read(); c != -1; c = br.read()) sb.append((char)c);
System.out.println(sb.toString());
Run Code Online (Sandbox Code Playgroud)
在这种情况下,指定编码很重要,因为您可能已经用UTF-8编辑了文件,然后将其放在jar中,打开文件的计算机可能将CP-1251作为其本机文件编码(例如) ; 所以在这种情况下你永远不会知道目标编码,因此显式编码信息至关重要.此外,通过char读取文件char的循环似乎效率低下,但它在BufferedReader上使用,因此实际上非常快.
Rag*_*air 12
您可以使用以下代码形式Java
new String(Files.readAllBytes(Paths.get(getClass().getResource("example.txt").toURI())));
Run Code Online (Sandbox Code Playgroud)
我喜欢 akosicki 对 Stupid Scanner Trick 的回答。这是我看到的最简单的没有外部依赖的 Java 8(实际上一直回到 Java 5)。如果您可以使用 Java 9 或更高版本(因为InputStream.readAllBytes()是在 Java 9 中添加的),这里有一个更简单的答案:
String text = new String(AppropriateClass.class.getResourceAsStream("foo.txt").readAllBytes());
Run Code Online (Sandbox Code Playgroud)
这是使用 Java 11 的解决方案Files.readString:
public class Utils {
public static String readResource(String name) throws URISyntaxException, IOException {
var uri = Utils.class.getResource("/" + name).toURI();
var path = Paths.get(uri);
return Files.readString(path);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
192940 次 |
| 最近记录: |