How do you read large text files in Java >20MB

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • N002213F
    New Member
    • Sep 2007
    • 38

    How do you read large text files in Java >20MB

    I have a Java text reader program that can easily read small text, but seems fails to read larger files >20MB. It seems to truncate the contents it reads.

    I there a way of making sure all the data is read.

    Thanks in advance.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    If you are sure there are no inefficiencies with the code, try using the classes in the nio package. They try to speed up things by using the OS itself.

    Comment

    • N002213F
      New Member
      • Sep 2007
      • 38

      #3
      Some sample code, hope its helps;


      Code:
      int startPos = "2008-10-15 13:00:00,640 ".length();
      BufferedReader br = new BufferedReader(new FileReader("c:\\temp\\test-prices_bulk.log"));
      PrintWriter pw = new PrintWriter("c:\\temp\\test-prices_NASDAQ.log");
      String line = null;
      while ((line = br.readLine()) != null) {
      	int endPos = line.indexOf("|");
      	if(endPos == -1 || endPos < startPos){
      		System.out.println("| not found or occurs earlier that expected.");
      		continue;
      	}
      	
      	String contractCode = line.substring(startPos, endPos);
      	if(contractCode.indexOf("NASDAQ") != -1){
      		pw.println(line);
      	}
      }
      pw.close();

      Comment

      • N002213F
        New Member
        • Sep 2007
        • 38

        #4
        Found the problem, but please promise you won't yell at me. I hadn't closed the PrintWriter.

        The maximum file size a Java program can read is dependent on the maximum memory you allocate to it when its starts. ~20MB is fairly small considering the default is 64MB
        Last edited by N002213F; Oct 16 '08, 11:48 AM. Reason: added more details

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by sipatha
          Found the problem, but please promise you won't yell at me. I hadn't closed the PrintWriter.

          The maximum file size a Java program can read is dependent on the maximum memory you allocate to it when its starts. ~20MB is fairly small considering the default is 64MB
          <Yelling>
          But your line 17 ...
          </Yelling>

          Comment

          • N002213F
            New Member
            • Sep 2007
            • 38

            #6
            @WideEyed
            Surprisingly i added it on the notepad when i was assembling the sample code, there is a lot that occurrs in between and didn't want to include it.

            @Aha
            I guess i should just stick to the notepad.

            Comment

            • N002199B
              New Member
              • Feb 2007
              • 41

              #7
              @I_Know_these_g uys_from_somewh ere
              This mistake reminds me of "No Problem"!!

              that is the effect of copy and paste sipatha! But nice going.

              Comment

              Working...