简体   繁体   English

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

[英]What to import to use IOUtils.toString()?

I am trying to use IOUtils.toString() to read from a file.我正在尝试使用 IOUtils.toString() 从文件中读取。 However, I am getting an error saying "IOUtils cannot be resolved."但是,我收到一条错误消息,提示“无法解析 IOUtils”。

What am I supposed to be importing to allow me to use this function?我应该导入什么才能使用这个 function?

String everything = IOUtils.toString(inputStream);

Thanks谢谢

import org.apache.commons.io.IOUtils; import 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. 此外,因为版本2.5的commons-io方法IOUtils.toString(inputStream)已被弃用。 You should use method with Encoding ie 你应该使用编码方法ie

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

Fryta's answer outline how to actually use IOUtils and snj's answer is 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() .如果您使用的是 java 9 或更高版本,并且您有输入 stream 可以阅读,您可以使用InputStream#readAllBytes() Just create a string from there and don't forget to specify charset.只需从那里创建一个字符串,不要忘记指定字符集。

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

import org.apache.commons.io.IOUtils;

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();
        }

The following import statement would be required:需要以下导入语句:

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

If the error " The import org.apache.commons.io cannot be resolved " shows up, add the commons-io-2.6.jar to the project Java buildpath and/or to the project lib folder .如果出现错误“ The import org.apache.commons.io cannot be resolved ”,请将commons-io-2.6.jar添加到项目 Java buildpath和/或项目lib 文件夹中。

Note: The suggestion to import "import org.apache.commons.io.IOUtils;"注意:建议导入“import org.apache.commons.io.IOUtils;” usually doesn't appear in Eclipse IDE at least in the context of this question.至少在这个问题的上下文中通常不会出现在 Eclipse IDE 中。

Here is code for How to convert InputStream to String in Java using Apache IOUtils 以下是如何使用Apache IOUtils将InputStream转换为Java中的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. 如果您需要更多帮助,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IOUtils.toString需要很长时间 - IOUtils.toString taking to long 大型流的IOUtils.toString()的替代方法? - Alternative to IOUtils.toString() for large streams? 相当于IOUtils.toString(InputStream)的番石榴 - Guava equivalent for IOUtils.toString(InputStream) IOUtils.toString() 异常 java.net.SocketTimeoutException: null - IOUtils.toString() exception java.net.SocketTimeoutException: null 在使用commons-io的IOUtils.toString(输入)后,是否需要手动关闭输入流? - Do I need to close the input stream manually after using IOUtils.toString(input) of commons-io? JAVA使用IOUtils.toString和HttpEntity.getContent()将InputStream转换为null - JAVA Using IOUtils.toString with HttpEntity.getContent() converting the InputStream to null 使用 `IOUtils.toString(containerRequestContext.getEntityStream(),“UTF-8”); 时无法解码特殊字符 ` - Cannot decode special character when using `IOUtils.toString(containerRequestContext.getEntityStream(),“UTF-8”); ` 不建议使用IOUtils.toByteArray-我们可以使用什么呢? - IOUtils.toByteArray deprecated - what can we use instead? IOUtils.read()之后InputStream会发生什么 - What happens to InputStream after IOUtils.read() 如何为 java.util.Arrays.toString 使用 static 导入? - How to use a static import for java.util.Arrays.toString?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM