Log in from a computer to take this course

You'll need to log in from a computer to start Learn Intermediate Java. But you can practice or keep up your coding streak with the Codecademy Go app. Download the app to get started.

apple storegoogle store
Learn

FileOutputStream and FileInputStream are two of Java’s built-in classes and can be extremely useful when your program is working with external files. FileInputStream is used to read data from a file into a program. Both these classes read and write in bytes and then convert to the variables a program or file requires accordingly.

First, we must import these classes into a program outside of the class using the following statements:

import java.io.*;

This statement also imports the IOException that we must throw or catch in our code in order to compile. We’ll dive into what they are later in the lesson.

Now, let’s take a look at how a program uses FileInputStream to write data into a file.

Step 1: Declare your input stream.

Declaring your input stream looks very similar to declaring an output stream. The statement FileInputStream input = new FileInputStream(filePath) will find the file at the specified file path.

  • An example of how an absolute path may look is FileInputStream input = new FileInputStream("C:/SampleFolder/input.txt")
  • An example of how a relative path may look is FileInputStream input = new FileInputStream("../documents/SampleFolder/input.txt")

Alternatively, you may choose to create an object of the variable type File to pass into the FileInputStream. This is useful if you plan to have the user pass in a path where they want to store the output. The code block below shows an example of how you may do this.

//Option 1: Pass file path/name directly to FileInputStream FileInputStream input1 = new FileInputStream("input.txt"); // Option 2: Use File object to pass file info to FileInputStream // Save file path that has been passed in by the user into a string variable. String fileName = args[0]; // Pass path to File object File inputFile = new File(fileName); // Pass File object to FileOutputSteam FileInputStream input2 = new FileInputStream(inputFile);

Step 2: Read your file.

Since FileInputStream uses bytes, we must convert the data we wish to read from a file into bytes. If we choose to read a file one byte at a time we may choose to store this in an int or char variable. The following code shows how to print each character in the file, which will effectively print the contents of the file as written.

// Counter variable to loop through the file int i = 0; // A loop to access each character while((i = inputFile.read()) != -1) { // Printing each character as it's reached System.out.print((char)i); }

Step 3: Close the file.

It is important we close a file once we are done reading it. To do this, we use the close() method.

input.close();

Instructions

1.

Import the entire io class at the top of your program, then throw an IOException in your main method.

2.

Declare a FileInputStream object using path, which points to the absolute path of input.txt.

3.

Now it’s time to access our file! To do so:

  • Create a counter variable set to 0
  • Write a while loop
4.

Finally, close your FileInputStream object.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?