Java 要使用 IOUtils.toString() 导入什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16535032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:21:27  来源:igfitidea点击:

What to import to use IOUtils.toString()?

javaapacheiotostring

提问by user2380101

I am trying to use IOUtils.toString() to read from a file. However, I am getting an error saying "IOUtils cannot be resolved."

我正在尝试使用 IOUtils.toString() 从文件中读取。但是,我收到一条错误消息,提示“无法解析 IOUtils”。

What am I supposed to be importing to allow me to use this function?

我应该导入什么才能使用此功能?

String everything = IOUtils.toString(inputStream);

Thanks

谢谢

回答by kracejic

import org.apache.commons.io.IOUtils;

import org.apache.commons.io.IOUtils;

回答by Himanshu Arora

Here is code for How to convert InputStream to String in Java using Apache IOUtils

这是如何使用 Apache IOUtils 在 Java 中将 InputStream 转换为 String 的代码

Reference : https://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/IOUtils.html

参考:https: //commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/IOUtils.html

FileInputStream fis = new FileInputStream(FILE_LOCATION);
String StringFromInputStream = IOUtils.toString(fis, "UTF-8");
System.out.println(StringFromInputStream);

Let me know if you need more help.

如果您需要更多帮助,请告诉我。

回答by Fryta

import org.apache.commons.io.IOUtils;

导入 org.apache.commons.io.IOUtils;

If you still can't import add to pom.xml:

如果仍然无法导入添加到 pom.xml:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>

or for direct jar/gradle etc visit: http://mvnrepository.com/artifact/commons-io/commons-io/2.5

或直接 jar/gradle 等访问:http://mvnrepository.com/artifact/commons-io/commons-io/2.5

Also since version 2.5 of commons-io method IOUtils.toString(inputStream) has been deprecated. You should use method with Encoding i.e.

此外,自 2.5 版的 commons-io 方法 IOUtils.toString(inputStream) 已被弃用。您应该使用 Encoding 方法,即

IOUtils.toString(is, "UTF-8");

回答by Sanjay Sharma

Alternatively you can try following way. It worked for me for reading public key for resource server

或者,您可以尝试以下方式。它适用于我读取资源服务器的公钥

        final Resource resource = new ClassPathResource("public.key");
        String publicKey = null;
        try {
            publicKey = new String(Files.readAllBytes(resource.getFile().toPath()), StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }

回答by thoredge

Fryta's answeroutline how to actually use IOUtils and snj's answeris good for files.

Fryta 的回答概述了如何实际使用 IOUtils,而snj 的回答适用于文件。

If you're on java 9 or later and you have an input stream to read you can use InputStream#readAllBytes(). Just create a string from there and don't forget to specify charset.

如果您使用的是 java 9 或更高版本并且您有一个要读取的输入流,则可以使用InputStream#readAllBytes()。只需从那里创建一个字符串,不要忘记指定字符集。

String s = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);