A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/io/fileinputstream_skip.htm below:

Java - FileInputStream skip(long n) method

Java - FileInputStream skip(long n) method Description

The Java FileInputStream skip(long n) method is used to skip over and discard n bytes of data from the input stream. This method is helpful when you want to move the file pointer forward by a specific number of bytes before reading data.

Declaration

Following is the declaration for java.io.FileInputStream.skip(long n) method −

public int skip(long n)
Parameters

n − The number of bytes to be skipped.

Return Value

The method returns the actual number of bytes skipped.

Exception Example - Usage of FileInputStream skip(long n) method

The following example shows the usage of Java FileInputStream skip(long n) method.

FileInputStreamDemo.java
package com.tutorialspoint;

import java.io.IOException;
import java.io.FileInputStream;

public class FileInputStreamDemo {
   public static void main(String[] args) throws IOException {
      FileInputStream fis = null;
      int i = 0;
      char c;
            
      try {
         // create new file input stream
         fis = new FileInputStream("test.txt");
         
         // skip bytes from file input stream
         fis.skip(4);
         
         // read bytes from this stream
         i = fis.read();
         
         // converts integer to character
         c = (char)i;
         
         // prints
         System.out.print("Character read: "+c);
   
      } catch(Exception ex) {
         // if any error occurs
         ex.printStackTrace();
      } finally {
         // releases all system resources from the streams
         if(fis!=null)
            fis.close();
      }
   }
}
Output Assumption

Assuming we have a text file test.txt in current directory, which has the following content. This file will be used as an input for our example program.

ABCDEF

Let us compile and run the above program, this will produce the following result−

Character read: E
Example - Skipping a Specific Number of Bytes

The following example shows the usage of Java FileInputStream skip(long n) method. This example reads data from a file using read(byte[] buf, int off, int len) method in chunks and prints it as text.

FileInputStreamDemo.java
package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;

public class FileInputStreamDemo {
   public static void main(String[] args) {
      try (FileInputStream fis = new FileInputStream("example.txt")) {
         System.out.println("Available bytes before skipping: " + fis.available());

         // Skipping 5 bytes
         long skippedBytes = fis.skip(5);
         System.out.println("Skipped Bytes: " + skippedBytes);

         // Reading the next byte after skipping
         int data = fis.read();
         System.out.println("Byte read after skipping: " + (char) data);

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
Output

Let us compile and run the above program, this will produce the following result−

Available bytes before skipping: 14
Skipped Bytes: 5
Byte read after skipping: ,
Explanation Example - Handling End of File (EOF)

The following example shows the usage of Java FileInputStream skip(long n) method. This example reads part of the file into a specific portion of an array.

FileInputStreamDemo.java
package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;

public class FileInputStreamDemo {
   public static void main(String[] args) {
      try (FileInputStream fis = new FileInputStream("example.txt")) {
         // Trying to skip more bytes than the file contains
         long skippedBytes = fis.skip(1000);
         System.out.println("Attempted to skip 1000 bytes, actually skipped: " + skippedBytes);

         // Checking if more data is available
         int data = fis.read();
         if (data == -1) {
            System.out.println("Reached end of file.");
         } else {
            System.out.println("Byte read after skipping: " + (char) data);
         }

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
Output(contents of example.txt)

Let us compile and run the above program, this will produce the following result−

Attempted to skip 1000 bytes, actually skipped: 1000
Reached end of file.
Explanation

java_io_fileinputstream.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4