JaVa
   

Reading Data from a File

In this next example, we will use the file that we created in the previous example and attempt to load it in and display the values that were retrieved from the file in the console window. First let's look at the complete code listing for this example console app. Then we will look into how the code works.

Code Listing 6-4: Reading data from a file
import java.io.*;
public class SimpleRead
{
 public SimpleRead()
 {
 // Reset players name and score...
 String playerName = "";
 int playerScore = 0;
 // Create our in file object...
 File theFile = new File("output.txt");
 try
 {
 // Create a data input stream for the file...
 DataInputStream inputStream = new DataInputStream(new
 FileInputStream(theFile));
 // Read the data from the input stream...
 // ->> The name...
 playerName = inputStream.readUTF();
 // ->> The score...
 playerScore = inputStream.readInt();
 // Close the input stream...
 inputStream.close();
 }
 catch(IOException e)
 {
 System.out.println(e);
 }
 System.out.println("The Players Name was: "+ playerName);
 System.out.println("And there score was: "+ playerScore);
 }
 public static void main(String args[])
 {
 SimpleRead mainApp = new SimpleRead();
 }
}


Java End example

When we execute this example with the output.txt file in the same directory that we are running it from, we can see the following output in the console window.

Java Click To expand
Screenshot-4: The data has been loaded back in

As you can see from this figure, the data has been loaded back in correctly from the file into our app. Let's now look at the code that we have used to make this work. First create the two variables in which we are going to eventually store the data that we retrieved from the file. We declare these two variables with the following two lines of code:

String playerName = "";
int playerScore = 0;


Next create a file object, as we did in the last example, to load in our output.txt file. This is accomplished with the following line of code:

File theFile = new File("output.txt");


Once we have this, attempt to create a DataInputStream object by creating a FileInputStream with our file object passed into the constructor. As with the DataOutputStream, the input stream can also throw an IOException, so as with the last example, encase all of the stream manipulation code within a try/catch block. Here is the code we use to create our DataInputStream object:

try
{
 DataInputStream inputStream = new DataInputStream(new
 FileInputStream(theFile));


Now that we have an input stream, we need to read the data back in the same order as we wrote it to the file. So the first thing we wish to read from the file is the string containing the player's name. This is done using the readUTF method, which reads a string that has been written in Unicode format (i.e., by the writeUTF method). This can be seen in the following line of code:

playerName = inputStream.readUTF();


So now we have the player's name back as a string. Finally, we need to retrieve the player's score, which can be done by simply using the readInt method, as the input stream maintains its current position in the file after previous reads. This can be seen in the following line of code:

playerScore = inputStream.readInt();


JaVa
   
Comments