A RetroSearch Logo

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

Search Query:

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

Java - FileInputStream Class

Java - FileInputStream Class Introduction

The Java FileInputStream class obtains input bytes from a file in a file system. What files are available depends on the host environment. Following are the important points about FileInputStream −

Class declaration

Following is the declaration for Java.io.FileInputStream class −

public class FileInputStream
   extends InputStream
Class constructors Sr.No. Constructor & Description 1

FileInputStream(File file)

This creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system.

2

FileInputStream(FileDescriptor fdObj)

This creates a FileInputStream by using the file descriptor fdObj, which represents an existing connection to an actual file in the file system.

3

FileInputStream(String name)

This creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file system.

Class methods Sr.No. Method & Description 1 int available()

This method returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.

2 void close()

This method closes this file input stream and releases any system resources associated with the stream.

3 protected void finalize()

This method ensures that the close method of this file input stream is called when there are no more references to it.

4 FileChannel getChannel()

This method returns the unique FileChannel object associated with this file input stream.

5 FileDescriptor getFD()

This method returns the FileDescriptor object that represents the connection to the actual file in the file system being used by this FileInputStream.

6 int read()

This method reads a byte of data from this input stream.

7 int read(byte[] b)

This method reads up to b.length bytes of data from this input stream into an array of bytes.

8 int read(byte[] b, int off, int len)

This method reads up to len bytes of data from this input stream into an array of bytes.

9 long skip(long n)

This method skips over and discards n bytes of data from the input stream.

Methods inherited

This class inherits methods from the following classes −

Example - Checking Available Bytes in a File

The following example shows the usage of Java FileInputStream available() method.

FileInputStreamDemo.java
package com.tutorialspoint;

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

public class FileInputStreamDemo {
   public static void main(String[] args) {
      File file = new File("example.txt");

      try (FileInputStream fis = new FileInputStream(file)) {
         // Get the number of available bytes at the beginning
         int availableBytes = fis.available();
         System.out.println("Bytes available to read: " + availableBytes);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
Output(if file size is 50 bytes)

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

Bytes available to read: 50
Explanation Example - Closing FileInputStream After Reading a File

The following example shows the usage of Java FileInputStream close() method.

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"); // Open file

         int data;
         while ((data = fis.read()) != -1) { // Read file byte by byte
            System.out.print((char) data);
         }

         fis.close(); // Close stream after reading
         System.out.println("\nFile closed successfully.");

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
Output(if example.txt contains Hello)

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

Hello
File closed successfully.
Explanation Example - Checking if a File Descriptor is Valid

The following example shows the usage of Java FileInputStream getFD() method.

FileInputStreamDemo.java
package com.tutorialspoint;

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

public class FileInputStreamDemo {
   public static void main(String[] args) {
      try (FileInputStream fis = new FileInputStream("example.txt")) {
         FileDescriptor fd = fis.getFD(); // Get file descriptor

         if (fd.valid()) {
            System.out.println("The file descriptor is valid.");
         } else {
            System.out.println("The file descriptor is not valid.");
         }

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
Output(if example.txt exists and opens correctly)

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

The file descriptor is valid.
Explanation

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