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_read.htm below:

Java - FileInputStream read() method

Java - FileInputStream read() method Description

The Java FileInputStream read() method reads a byte of data from this input stream. The method blocks if no input is available.

Declaration

Following is the declaration for java.io.FileInputStream.read() method −

public int read()
Parameters

NA

Return Value

The methods returns the next byte of data, or -1 if the end of the file is reached.

Exception

IOException− If any I/O error occurs.

Example - Usage of FileInputStream read() method

The following example shows the usage of Java FileInputStream read() 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");
         
         // read till the end of the file
         while((i = fis.read())!=-1) {
         
            // converts integer to character
            c = (char)i;
            
            // prints character
            System.out.print(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−

ABCDEF
Example - Using read() to Read a Single Byte

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

FileInputStreamDemo.java
package com.tutorialspoint;

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

public class FileInputStreamDemo {
   public static void main(String[] args) {
      try {
         // Open a file input stream
         FileInputStream fis = new FileInputStream("example.txt");

         // Read and print each byte until the end of the file (-1)
         int byteData;
         while ((byteData = fis.read()) != -1) {
            // Convert byte to character and print
            System.out.print((char) byteData);
         }

         // Close the stream
         fis.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
Output(contents of example.txt)

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

Hello, World!
Explanation Example - Using read(byte[] b) to Read Multiple Bytes

The following example shows the usage of Java FileInputStream read(byte[] b) method.

FileInputStreamDemo.java
package com.tutorialspoint;

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

public class FileInputStreamDemo {
   public static void main(String[] args) {
      try {
         // Open a file input stream
         FileInputStream fis = new FileInputStream("example.txt");

         // Create a byte array buffer
         byte[] buffer = new byte[10]; // Read 10 bytes at a time
         int bytesRead;

         // Read chunks of bytes into the buffer
         while ((bytesRead = fis.read(buffer)) != -1) {
            // Convert bytes to string and print
            System.out.print(new String(buffer, 0, bytesRead));
         }

         // Close the stream
         fis.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
Output(contents of example.txt)

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

Hello, World!
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