Search code examples
javautf-8bufferedreaderfileinputstream

Java: reading utf-8 file page by page using FileInputStream


I need some code that will allow me to read one page at a time from a UTF-8 file.

I've used the code;

 File fileDir = new File("DIRECTORY OF FILE");
 BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileDir), "UTF8"));
 String str;
 while ((str = in.readLine()) != null) {
        System.out.println(str);
   }
 in.close();
 } 

After surrounding it with a try catch block it runs but outputs the entire file! Is there a way to amend this code to just display ONE PAGE of text at a time? The file is in UTF-8 format and after viewing it in notepad++, i can see the file contains FF characters to denote the next page.


Solution

  • You will need to look for the form feed character by comparing to 0x0C.

    For example:

    char c = in.read();
    while ( c != -1 ) {
       if ( c == 0x0C ) {
         // form feed
       } else {
         // handle displayable character
       }
    
       c = in.read();
    }
    

    EDIT added an example of using a Scanner, as suggested by Boris

        Scanner s = new Scanner(new File("a.txt")).useDelimiter("\u000C");
        while ( s.hasNext() ) {
            String str = s.next();
    
            System.out.println( str );
        }