Skipping part of an InputStream when reading from it

You can do so using the method skip. You can call this method from any point where you are in the inputstream.

The following program skips half of the file, where the filename is supplied at command line.

Main.java:

import java.io.*;
  
public class Main {
   public static void main(String args[]) throws Exception {
      if (args.length != 1) {
         System.err.println("Usage: java Main <file.txt>");
         System.exit(1);
      }
 
      File file = new File(args[0]);
      BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
 
      int half = (int) file.length() / 2;
      bis.skip(half);
 
      int b;
      while ((b = bis.read()) > 0) {
         System.out.print((char) b);
      } 
   }
}