Open In App
Related Articles

Java Program to Read Text From File From a Specified Index

Improve Article
Save Article
Like Article

In a file system, without reading the previous text we cannot directly access the specific index. Thus, Reading text from a file from the specific index is achieved by skipping all the previous characters of a specified index. To read text from an index n, we need to skip (n-1) bytes. Here, we will use  FileInputStream class to read text from the file.

long skip(long n): Skips over and discards n bytes of data from the input stream.


public long skip(long n) throws IOException

Parameters: n — the number of bytes to be skipped.

Returns: The actual number of bytes skipped.

Throws: IOException


// Java program to read text from file from a specified
// index
public class GFG {
    public static void main(String args[])
        try {
            // attach the file to FileInputStream
            FileInputStream fin = new FileInputStream(
                "C:\\Users\\ASPIRE\\Desktop\\java folder\\Demo.txt");
            int i = 0;
            // discards 7 bytes of data from the input
            // stream.
            // read from the file
            System.out.print("Printing text from index 8: ");
            while ((i = != -1) {
        catch (Exception e) {


Demo.txt file:


Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 05 Feb, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials