English 中文(简体)
如何阅读大文本档案——java [封闭]
原标题:How to read large text files - java [closed]

I am making a game, but to install it, it requires 7zip to unpack the files, so I have included the 7zip installer. I have created a JFrame with a JTextArea in it to input the 7zip icense, but I cant get BufferedReader to read the whole txt file (its 57 lines, and I think its mainly because Bufferedreader wasnt designed to read that many lines.) Can you please help me read the file so I can add the license into the game. Thanks, Jackson

EDIT I feel so loved when you guys pay out a newb for not knowing things -_-

最佳回答

我最近写了一个节目,读到与BufferedReader合用文档中的11亿条线。

阅读整个档案的最简单方式是使用57条字。

String text = FileUtils.readFileToString(new File("uncompressedfile.txt"));

String text = FileUtils.readFileToString(new File("uncompressedfile.txt"), "UTF-8");

或 if compressed with gzip (similarly with 7zip)

String text = IOUtils.toString(new GZipInputStream("compressedfile.txt.gz"));
问题回答

仅读到档案中的完整案文。 将其储存在<代码>String 可变中,然后将这一数值输入JTextArea,因为57条线并不大,可以储存在证书上。

您可以采取两种方式:

1>Using Scanner

void read() throws IOException {
    StringBuilder text = new StringBuilder();
    String NL = System.getProperty("line.separator");
    Scanner scanner = new Scanner(new FileInputStream(fFileName), fEncoding);
    try {
      while (scanner.hasNextLine()){
        text.append(scanner.nextLine() + NL);
      }
    }
    finally{
      scanner.close();
    }
    log("Text read in: " + text);
  }

2>

static public String getContents(File aFile) {

    StringBuilder contents = new StringBuilder();

    try {

      BufferedReader input =  new BufferedReader(new FileReader(aFile));
      try {

        while (( line = input.readLine()) != null){
          contents.append(line);
          contents.append(System.getProperty("line.separator"));
        }
      }
      finally {
        input.close();
      }
    }
    catch (IOException ex){
      ex.printStackTrace();
    }

    return contents.toString();
  }

57条线并不是说,巨大的缓冲器被用在纸浆中阅读档案:





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签