0

So I was trying to read a text file of approx 40KB using Buffered Reader in android. The thing is one of the line (mostly last line) from the file exceeds 9000 characters which is difficult to store in String and to log it.

I tried this approach below but as characters exceed it discards parsing the remaining part from the line.

  try {   
       File root = android.os.Environment.getExternalStorageDirectory();
       File file = new File (root.getAbsolutePath() + "/" + "new.txt");

       BufferedReader r = null;
       r = new BufferedReader(new FileReader(file));
       StringBuilder total = new StringBuilder();
       String line;
       while((line = r.readLine()) != null) {
          Log.e("Line",line);
          total.append(line);
        }
        r.close(); 
     } catch (Exception e) {
        e.printStackTrace();
  }

To which I thought to change String line to String line = new String(new byte[1024*1024]) could solve my problem. But Android Studio is highlighting this as reductant code. The thing is I need to apply some regex stuff on each line in while loop.

Is there any workaround I can use. By the way here is my 40 KB file link https://www.dropbox.com/s/hp7vn6vt86adv6g/new.txt?dl=0

Edit: The file I am trying to parse is an html file.

Updated

I was wrong, the string in the line is not omitting the rest part as suggested by skandigraun (from comments). Logger was not printing the whole string because it was exceeding it's 4000 chars limit while my string was 8093 chars.

In short above code is working just as fine!

kaustubhpatange
  • 442
  • 5
  • 13
  • Are you logging it to console? If so, then computing line breaks can take quite an effort, logging into a file would be faster. – skandigraun Feb 28 '19 at 16:34
  • Actually I am logging it to logger, but last line always prints half I mean it omits the rest. – kaustubhpatange Feb 28 '19 at 16:37
  • Yeah, logcat goes only to about 4000 characters/call. To show everything, you need to call it multiple times for long strings, like [this](https://stackoverflow.com/questions/24636089/android-how-to-view-long-string-in-logcat/24636147) – skandigraun Feb 28 '19 at 16:42
  • Okay so you are saying my string contains all 9000+ characters from last line but logcat is not showing them all (4000 as you say). If this is true then you can check if this string contains 'uc-download-link'. I tried this but that string doesn't contain it. – kaustubhpatange Feb 28 '19 at 16:44
  • Make sure that you are checking the correct line. The line that contains that substring is only 8093 chars long. – skandigraun Feb 28 '19 at 17:00
  • Yeah that's right, but it doesn't contain that word 'uc-download-link'. I'll check again if so! – kaustubhpatange Feb 28 '19 at 17:37

0 Answers0