FileInputStream skip() Method in Java with Examples
Last Updated :
01 Dec, 2021
FileInputStream class is quite helpful to read data from a file in the form of a sequence of bytes. FileInputStream is meant for reading streams of raw bytes such as image, audio, video, etc. For reading streams of characters, FileReader is recommended. It was firstly introduced in JDK 1.0.
FileInputStream skip() method
The skip(n) method in FileInputStream is quite helpful as it discards the n bytes of data from the beginning of the input stream. We can understand its working by considering the following example,
We are reading a stream of characters through FileInputStream, and we want to read it after skipping the first eight characters from the stream. Let the string be "GeeksforGeeks", so after skipping the first eight characters, our string will become "Geeks".
The skip() method is useful to achieve the task as mentioned above. This method is defined in FileInputStream class of the Java.io package.
Java I/O package helps the user to perform all the input-output operations. These streams support all objects, data types, characters, files, etc., to execute the I/O operations fully.
Syntax:
Input_File_Stream.skip(n)
Input_File_Stream: The file input stream.
Parameters:
- n − A Non-negative integer.
- Bytes to be skipped from a stream of characters.
Return Value: It returns the actual number of bytes skipped.
Exception: IOException − If n is a negative integer or I/O error occurs.
Example 1:
Java
// Java program to demonstrate the working
// of skip() method in FileInputStream
// Importing the class files
// defined under io Package
import java.io.FileInputStream;
import java.io.IOException;
// Public class
public class GeeksforGeeks {
// Main method
public static void main(String[] args)
throws IOException
{
FileInputStream inputFileStream = null;
// Variables to store the fifth character
int integerValue = 0;
char characterValue;
try {
// Create new file input stream
inputFileStream = new FileInputStream(
"C:\\Users\\harsh\\Desktop\\GFG\\inputFile.txt");
// Skip 4 bytes from the beginning
// in the file input stream
inputFileStream.skip(4);
// Read bytes from this stream
// (first character only)
integerValue = inputFileStream.read();
// Converting integer to character type
characterValue = (char)i;
// Print the character
System.out.print("Character read: "
+ characterValue);
}
catch (Exception exception) {
// If any error occurs
exception.printStackTrace();
}
finally {
// Releasing all system resources
if (inputFileStream != null)
inputFileStream.close();
}
}
}
// This code is contributed by Bhuwanesh
Steps for Compilation in a Local System:
1. We have saved the above program with the name "GeeksforGeeks" locally:
Source code file
2. Lets now create a text file named "inputFile":
Text file
3. We need to enter some text inside the created text file. As an example, we have written "GeeksforGeeks" in the text file.
Text file data
4. We will now compile our program through the javac compiler using the command prompt:
javac GeeksforGeeks.java
Compilation of the source code file
5. As you can see below GeeksforGeeks.class byte code file is generated. Now run the program using the following command:
java GeeksforGeeks
Output:
Output
Output Description: Since we have passed 4 as an argument to the skip() function in the program. As we can see in the program's output, the first four characters (or bytes) are skipped ('G', 'e', 'e', 'k' ). The fifth character in GeekforGeeks is 's' hence, 's' is printed on the console.
Example 2:Â
Java
// Java program to demonstrate the working
// of skip() method in FileInputStream
// Importing the FileInputStream class
import java.io.FileInputStream;
// Importing the IOException class
import java.io.IOException;
// Public class
public class GeeksforGeeks {
// Main method
public static void main(String[] args)
throws IOException
{
FileInputStream inputFileStream = null;
int integerValue = 0;
char characterValue;
try {
// Create new file input stream
// Give the full path of the input file
inputFileStream = new FileInputStream(
"C:\\Users\\harsh\\Desktop\\GFG\\inputFile.txt");
// Skip 8 bytes from the beginning in the file
// input stream
inputFileStream.skip(8);
// Print on console
System.out.print("String read: ");
// Iterate till EOF is not reached
while ((integerValue = inputFileStream.read())
!= -1) {
// converts integer to character
characterValue = (char)i;
// prints character
System.out.print(characterValue);
}
}
catch (Exception exception) {
// If any error occurs
exception.printStackTrace();
}
finally {
// Releasing all system resources
if (inputFileStream != null)
inputFileStream.close();
}
}
}
// This code is contributed by Bhuwanesh
In order to compile the above program in a local system, We can follow the same steps that we have mentioned in Example 1. After the compilation, we can run the program, and it generates the following output.
Output:
Output
Output Description: As you can see in the output first eight characters are skipped ('G', 'e', 'e', 'k', 's', 'f', 'o', 'r'). The remaining string is "Geeks" so it gets printed on the console.
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read