Open In App

Java Program to Read Text From File From a Specified Index

Last Updated : 05 Feb, 2021
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:


Similar Reads

Java Program to Read a Large Text File Line by Line
As we are well verse with this topic let us put more stress in order to figure out minute differences between them. Here we are supposed to read from a file on the local directory where a text file is present say it be 'gfg.txt'. Let the content inside the file be as shown below: Geeks for Geeks. A computer science portal. Welcome to this portal. H
3 min read
Java Program to Read Content From One File and Write it into Another File
File handling plays a major role in doing so as the first essential step is writing content to a file. For this is one must know how to write content in a file using the FileWriter class. The secondary step is reading content from a file and print the same. For this, one must have good hands on File Reader class to do so. Now in order to read conte
5 min read
Java Program to Create a File in a Specified Directory
Creating a file in a specific directory using Java can be done in various ways. This is done using predefined packages and Classes. In this article, we will learn about how to create a file in a specified directory. Methods to Create a File in a Specified DirectoryThree such methods to create a file in a specified directory are mentioned below: Fil
3 min read
Removing Element from the Specified Index in Java ArrayList
The remove(int index) method present in java.util.ArrayList class removes the element at the specified position in this list and shifts any subsequent elements to the left (i.e. subtracts one from their indices). Syntax : public removed_element remove(int index) Parameters: The index of the element to be removed. Return Type: This method returns th
3 min read
Removing Element from Specified Index in Java Vector
Element at the specified index in Java vector can be removed using remove() method. this method is present in java.util.Vector class. We can pass index of the element which will be removed as an argument. remove() method return method returns the element that was removed. Syntax : public removed_element remove(int index) Parameters: The index of th
2 min read
Insert all Elements of Other Collection to Specified Index of Java ArrayList
ArrayList is part of the collection framework. It is a List and implements the java.util.list interface. ArrayList is a better alternative to Arrays, especially if you are not sure about the array size. Unlike array which has a fixed size, ArrayList can grow in size when needed. Internally ArrayList also uses arrays to store data. When it reaches t
2 min read
Java program to read all mobile numbers present in given file
Given the input text file, read all mobile numbers present and write to some output file. Suppose we have a file names as input.txt and in that input file we have some Mobile Number which are mixed with some other data. Our task is to read the input file line by line and if we find any Number in that line we have to write that Mobile Number to an a
2 min read
Java Program to Make a File Read-Only
Read-Only is the file attribute that the operating system assigns to the file. When the file is flagged as read-only, it means that it can only be opened or read, one cannot change the name of the file, can not rewrite or append the content of the file, and also cannot delete the file. Method 1: Using setLastModified Method To make the file read-on
2 min read
Java Program to Read and Print All Files From a Zip File
A zip file is a file where one or more files are compressed together, generally, zip files are ideal for storing large files. Here the zip file will first read and at the same time printing the contents of a zip file using a java program using the class for marking the zip file and after reading it, the contents inside it wou
4 min read
Java Program to Read a File to String
There are multiple ways of writing and reading a text file. This is required while dealing with many applications. There are several ways to read a plain text file in Java e.g. you can use FileReader, BufferedReader or Scanner to read a text file. Given a text file, the task is to read the contents of a file present in a local directory and storing
8 min read
Article Tags :
Practice Tags :