ADVERTISEMENT
ADVERTISEMENT

Java program to read text from file from a specified index or skipping byte using FileInputStream

In this program, we are going to learn how to read text from a file after skipping some bytes and print them on the output screen using FileInputStream.
Submitted by IncludeHelp, on October 19, 2017

Given a file with some text and we have to print text after skipping some bytes using FileInputStream in Java.

There is a file named "IncludeHelp.txt" which is stored at my system in "E:" drive under "JAVA" folder (you can choose your path), and file contains the following text,

The quick brown fox jumps over the lazy dog.

Example:

File's content is: "The quick brown fox jumps over the lazy dog."
After skipping starting 10 bytes...
Output: "brown fox jumps over the lazy dog."

Consider the program

import java.io.*;

public class Skip {
  public static void main(String[] args) {
    //file class object     
    File file = new File("E:/JAVA/IncludeHelp.txt");

    try {
      FileInputStream fin = new FileInputStream(file);
      int ch;

      System.out.println("File's content after 10 bytes is: ");

      //skipping 10 bytes to read the file
      fin.skip(10);

      while ((ch = fin.read()) != -1)
        System.out.print((char) ch);
    } catch (FileNotFoundException ex) {
      System.out.println("FileNotFoundException : " + ex.toString());
    } catch (IOException ioe) {
      System.out.println("IOException : " + ioe.toString());
    } catch (Exception e) {
      System.out.println("Exception: " + e.toString());
    }
  }
}

Output

File's content after 10 bytes is: 
brown fox jumps over the lazy dog.

Java File Handling Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.