Various read-line methods failing on Android

OK, I'm officially frustrated with file handling on the Android. I'm new to it (only a few days) so I might just be missing something obvious. I have successfully written a short file of multiple lines with CSV data on each line. That's not the problem; reading is. I started with this (leaving out the exception handling and parsing for clarity):

        FileInputStream in = context.openFileInput("foo.txt");
        InputStreamReader isr = new InputStreamReader( in );
        BufferedReader buffreader = new BufferedReader( isr );
        String inline;
        while( (inline = buffreader.readLine())!=null)
        {
            // parse CSV here
        }

I got an immediate null when reading the line. So I worked back up the line and tried reading raw bytes using various examples in SO as a pattern. I won't reproduce all the fails, but things like this didn't work either:

        FileInputStream in = context.openFileInput("foo.txt");
        InputStreamReader isr = new InputStreamReader( in );
        BufferedReader buffreader = new BufferedReader( isr );
        CharBuffer buff = CharBuffer.allocate(1024);
        isr.read(buff);
        String s = buff.toString();

Debugging showed that the character buffer and string had consumed characters, but they were blank. Well, I can truly go old-school (I programmed in C before it was a decade old) if necessary. The following worked:

        FileInputStream in = context.openFileInput("foo.txt");
        InputStreamReader isr = new InputStreamReader( in );
        int c;
        String foo = ""; 
        while( (c = isr.read())>=0)
            foo += (char)c;

"foo" managed to have the whole contents which I can split up and process, but I dislike that it's so coarse. Yes, it works, but I'd like to know why the others didn't. I tried to stick closely to the various examples in SO, but had no success.

Any ideas about what might be wrong with the first attempts?

Answers


          do {
              inline=bruffreader.readLine();
              if(inline != null) {
                 //parse here
              }
          } while (inline != null);

worked for me, it didn't return null


Need Your Help

DD/MM/YYYY input validation

c++ linux date validation

I am working on a date validation (MM/DD/YYYY) and getting this error:

How to order self-referencing xml

sorting xslt xpath self-reference

I have a list of order lines with each one product on them. The products in may form a self-referencing hierarchy. I need to order the lines in such a way that all products that have no parent or w...