简体   繁体   English

找不到符号IOUtils

[英]Cannot find symbol IOUtils

Basically I am trying to take information from a text file and turn it into a string. 基本上,我试图从文本文件中获取信息并将其转换为字符串。 The code I have is: 我的代码是:

FileInputStream inputStream = new FileInputStream("filename.txt");
try 
{
  String everything = IOUtils.toString(inputStream);
} 
finally 
{
  inputStream.close();
}

the error message I get is --> 我收到的错误消息是->

java:53: cannot find symbol
symbol  : class IOUtils
location: class CheckSystem

I assumed this was because of my imports, but I have io and util and even text imported (just as below) 我认为这是因为我的导入,但是我导入了io和util甚至输入了文本(如下所示)

import java.util.*;

import java.text.*;

import java.io.*;

Why can't I access the IOUtils class and its methods? 为什么我不能访问IOUtils类及其方法? If that cannot be answered, an alternative but very simple means of reading a text file into a string would be fine. 如果无法回答,那么将文本文件读取为字符串的一种替代方法却非常简单,那就很好了。

You don't need anything outside of standard JDK to read from a text file easily and efficiently. 您不需要标准JDK之外的任何内容即可轻松高效地从文本文件中读取内容。 For example you can do so like this: 例如,您可以这样做:

BufferedReader br = new BufferedReader(new FileReader("file.txt"));
try {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();

    while (line != null) {
        sb.append(line);
        sb.append(System.lineSeparator());
        line = br.readLine();
    }
    String everything = sb.toString();
} catch(IOException e) {
  }
  finally {
    br.close();
}

taken from: Reading a plain text file in Java 摘自: 用Java读取纯文本文件

The everything String contains the contents of the file.txt , which must be located in the same directory as where the java class file is being run from. 字符串的everything内容everything包含file.txt的内容,该内容必须与运行java类文件的目录位于同一目录中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM