1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
/* Skip n bytes while reading the file using FileInputStream This example shows how to skip n bytes while reading the file using skip method of Java FileInputStream class. */ import java.io.*; public class SkipBytesReadFile { public static void main(String[] args) { //create file object File file = new File("C://FileIO//ReadString.txt"); try { //create FileInputStream object FileInputStream fin = new FileInputStream(file); int ch; /* * To skip n bytes while reading the file, use * int skip(int nBytes) method of Java FileInputStream class. * * This method skip over n bytes of data from stream. This method returns * actual number of bytes that have been skipped. */ //skip first 10 bytes fin.skip(10); while( (ch = fin.read()) != -1 ) System.out.print((char) ch); } catch(FileNotFoundException e) { System.out.println("File not found" + e); } catch(IOException ioe) { System.out.println("Exception while reading the file " + ioe); } } } |
Add Comment