C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programs » Java file handling programs

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.






COMMENTS