Q:

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

belongs to collection: Java File Handling Programs

0

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."

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java File Handling Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to check whether a file is hidden or ... >>
<< Java program to determine number of bytes written ...