How to read data from text file using java program

import java.io.FileInputStream;

public class DataStreamExample {
  public static void main(String args[]) {
    try {
      FileInputStream fin = new FileInputStream("D:\\TechSarvam.txt");
      int i = fin.read();
      System.out.print((char) i);
      fin.close();
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}

Step-by-Step Breakdown:

1. Importing FIileInputStream Class

import java.io.FileInputStream;

2. Main Method:

public class DataStreamExample {

public static void main(String args[]) {

The main method is the entry point for the execution of this program.

3. Creating FileInputStream Object:

FileInputStream fin = new FileInputStream(“D:TechSarvam.txt”);

4. Reading a Single Byte from the File:

int i = fin.read();

  • read() method reads one byte of data from the file and returns that integer.
  • In Java, characters are represented in bytes. The function read() returns an integer representing the byte.
  • If the file is empty, then read() will return -1.
  • The integer value representing the byte is stored in the variable i.

5. Print the Character Represented by the Byte:

System.out.print((char) i);

  • The integer value i from byte is converted into a character with the keyword (char).
  • This interprets the byte to its associated character (ASCII value).
  • In case if a byte has read as 65, then this will convert it to ‘A’.

6. Closing of Input File

fin. close ();

  • It is very necessary to close FileInputStream once the operations regarding the files have been accomplished.
  • close() is used to release system resources which have been associated with the input stream.

7. Exception Handling:

  }

  } catch(Exception e) {

      System.out.println(e);

  }

If there is an exception (for instance, if a file does not exist or it contains read errors), it will be caught, and an error message will be printed.

Output:

  • The program will read the first byte of the file TechSarvam.txt and print it as a character.
  • For example if the first byte in the file is 84 which correspond to the ascii character ‘T’, The program will display:

  T

  • If first byte is 65 or correspond to char ‘A’, it’ll print:

 A

  • If a file named TechSarvam.txt can’t be read or the wrong file path/permission to file is there (say, so that reading in the file TechSarvam.txt) an exception occurs and error prints.

Example Output:

1. If the first byte in file is 84, then,

Output of the program above will be:

T

2. If an exception occurs, for example, the file does not exist, a possible output could be

java.io.FileNotFoundException: D:TechSarvam.txt (The system cannot find the file specified).

Summary:

  • The program demonstrates how to read a file byte by byte using a Java FileInputStream.
  • It reads the first byte of the file and prints the character corresponding to it.
  • Closes the input stream after reading the file: Proper resource management
  • It catches exceptions if the file does not exist or any other I/O issues occur.

Leave a Comment

Your email address will not be published. Required fields are marked *