2012-05-13 232 views
-2

我正在做一个游戏,但要安装它,它需要7zip来解压文件,所以我已经包含了7zip安装程序。我创建了一个带有JTextArea的JFrame来输入7zip icense,但是我不能让BufferedReader读取整个txt文件(它的57行,我认为它主要是因为Bufferedreader没有设计读取那么多行)。帮助我阅读文件,以便将许可证添加到游戏中。 谢谢, 杰克逊如何阅读大文本文件 - java

编辑 我觉得这样爱当你们不知道的东西交出来对于新手-_-

+3

什么是 '可以' t'BufferedReader'读取'实际上是什么意思?而自57年以来,“大文本文件”呢?我用'BufferedReader'来读取数百万行。事实上,你的声明并非设计为阅读57。 – EJP

+2

向我们展示您尝试过的。 –

+0

@EJP唯一的教程,我可以找到只显示我如何阅读1行,而不是57 –

回答

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")); 
+0

我从哪里导入FileUtils? –

+1

它的一部分Apache Common的IO http://commons.apache.org/io/apidocs/org/apache/commons/io/IOUtils.html –

+0

谢谢。我下载了它,除了现在得到这个异常外,它全部正常工作:java.nio.charset.UnsupportedCharsetException:7zip_license.txt –

1

只是从文件中读取完整文本。将其存储到String变量中,然后将该值放入JTextArea,因为57行对于存储JVM的内存来说并不那么重要。

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

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的:)