如何阅读大文本文件 - java

时间:2012-05-13 12:29:58

标签: java text 7zip

我正在制作游戏,但要安装它,需要7zip来解压缩文件,所以我已经包含了7zip安装程序。我在其中创建了一个带有JTextArea的JFrame来输入7zip icense,但是我无法让BufferedReader读取整个txt文件(它的57行,我认为它主要是因为Bufferedreader并不是为了读取那么多行而设计的。你能帮我看一下这个文件,这样我就可以把许可证添加到游戏中。 谢谢, 杰克逊

EDIT 当你们因为不知道事情而付出了新的话时,我感到很受欢迎-_-

3 个答案:

答案 0 :(得分:1)

只需阅读文件中的完整文字即可。将它存储到String变量中,然后将该值放入JTextArea,因为57行并不是存储在JVM内存中的那么大。

答案 1 :(得分:1)

我最近编写了一个程序,它使用BufferedReader从gzip文件中读取了11亿行。

读取小到57行的整个文件的最简单方法是使用

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

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

或者使用gzip压缩(类似于7zip)

String text = IOUtils.toString(new GZipInputStream("compressedfile.txt.gz"));

答案 2 :(得分:0)

您可以通过两种方式实现: -

1>使用扫描仪

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> BufferedReader

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行不是那么大,bufferedreader已用于读取gb中的文件:)

相关问题