-2

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

  • 3
    What does 'can't get `BufferedReader` to read' actually mean? And since when is 57 lines a 'large text file'? I have used `BufferedReader` to read millions of lines. Your claim that it wasn't designed to read 57 has no basis in fact. – user207421 May 13 '12 at 12:32
  • @EJP the only tutorials I can find only showed me how to read 1 line, not 57 – Black_Sirrah239 May 13 '12 at 12:34
  • try this: http://www.java2s.com/Tutorial/Java/0180__File/ReadingTextfromaFile.htm – sschrass May 13 '12 at 12:36
  • @Black_Sirrah239 Irrelevant. Reading 57 lines, or a million, is the same as reading one line. There are loops. You could even replicate bufferedReader.readLine() 57 times, or a million, if the code will fit. – user207421 May 13 '12 at 13:59

3 Answers3

1

Just read complete text from the file. store it into a String variable and then put that value into the JTextArea, because 57 lines is not that much huge to store in memory of JVM.

Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86
1

I recently write a program which read 1.1 billion lines from gzipped files with BufferedReader.

The simplest way to read a whole files as small as 57 lines is to use

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

or

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

or if compressed with gzip (similarly with 7zip)

String text = IOUtils.toString(new GZipInputStream("compressedfile.txt.gz"));
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • where do I import FileUtils from? – Black_Sirrah239 May 13 '12 at 13:02
  • 1
    Its part of Apache Common's IO http://commons.apache.org/io/apidocs/org/apache/commons/io/IOUtils.html – Peter Lawrey May 13 '12 at 13:08
  • thanks. I downloaded it, it was all working except now im getting this exception: java.nio.charset.UnsupportedCharsetException: 7zip_license.txt – Black_Sirrah239 May 13 '12 at 13:32
  • You can provide either one or two arguments. If you provide two arguments, the second must be the character set (as its says in the javadoc) The simplest option is to provide just the filename (as in the example above). BTW It is considered best practice to specify a character set like "UTF-8", but I would get the simple case working first. – Peter Lawrey May 13 '12 at 13:36
  • I currently have 'String ZipLicenseData = FileUtils.readFileToString("7zip_license.txt");' and I cant workout why it wont work – Black_Sirrah239 May 13 '12 at 13:42
  • I fixed the example, but I don't know why you are getting an exception re the character set. You could try specifying it but you shouldn't need to. (See the updated examples) – Peter Lawrey May 13 '12 at 13:49
  • Thanks! Your updated example works, except its FileUtils.readFileToString not FileUtils.readFileAsString. – Black_Sirrah239 May 13 '12 at 13:52
0

You can do it in two ways :-

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>The 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 lines is not that huge , bufferedreader has been used to read files in gb's:)

Abhishek Singh
  • 6,068
  • 1
  • 23
  • 25