Import java



Input from File

Goal: Learn to read information (data) from a file

Your tasks:

1. Use Nodepad to create a file to store the US states and their adjacent ones. Each row contains a state and all its adjacent states. Name the file data.txt.

2. Write a Java program to read data.txt and extract state by state and put them in a double linked list. Submit your work with a proper write-up.

Hints:

1. Each file in Java will have an extra backslash because single backslash is used already. For example ("A:\\data.txt").

2. File open must be close if no longer in use.

// open a file

File inFile = new File ("A:\\data.txt");

FileReader fileReader = new FileReader(inFile);

BufferedReader bufReader = new BufferedReader(fileReader);

>

//close the file

bufReader.close();

3. In the method using file, you need to “throws IOException” to avoid the errors related to the file such as file name is not existed, reading beyond end of file, or the directory does not existed.

import java.io.*;

class ReadFile

{

// no components and no constructors

// main method

public static void main(String args[]) throws IOException

{// main

// open a file

File inFile = new File ("A:\\data.txt");

FileReader fileReader = new FileReader(inFile);

BufferedReader bufReader = new BufferedReader(fileReader);

// Vararible to hold one line of the input file

String str;

// get a line and display

str = bufReader.readLine();

System.out.println(str);

//close the file

bufReader.close();

}//End Main

} // end ReadFile

4. The following program demonstrates a loop to read line by line. It intends to read beyond end of file. Since it has “throws IOException, the program is not interrupted. In this case it will read a null string. Hence the program is used this information to terminate the loop. I provide several versions of the loop for your comparison.

// VERSION 1

import java.io.*;

class ReadFile

{

// no components and no constructors

// main method

public static void main(String args[])throws IOException

{// main

// open a file

File inFile = new File ("A:\\data.txt");

FileReader fileReader = new FileReader(inFile);

BufferedReader bufReader = new BufferedReader(fileReader);

// Variable to hold one line of the input file

String str = bufReader.readLine();

while (str !=null)

{

System.out.println(str);

// get a next line

str = bufReader.readLine();

}

//close the file

bufReader.close();

}//End Main

} // end ReadFile

// VERSION 2

import java.io.*;

class ReadFile

{

// no components and no constructors

// main method

public static void main(String args[])throws IOException

{// main

// open a file

File inFile = new File ("A:\\data.txt");

FileReader fileReader = new FileReader(inFile);

BufferedReader bufReader = new BufferedReader(fileReader);

// Variable to hold one line of the input file

String str;

do

{

// get a line and display

str = bufReader.readLine();

System.out.println(str);

} while (str != null);

//close the file

bufReader.close();

}//End Main

} // end ReadFile

A:\>javac openfile.java

A:\>java ReadFile

Just For Today

Life is wonderful.

Who are you?

To be or not to be?

Wow! who am I?

Null

// VERSION 3

import java.io.*;

class ReadFile

{

// no components and no constructors

// main method

public static void main(String args[])throws IOException

{// main

// open a file

File inFile = new File ("A:\\data.txt");

FileReader fileReader = new FileReader(inFile);

BufferedReader bufReader = new BufferedReader(fileReader);

// Variable to hold one line of the input file

String str = bufReader.readLine();

while (str !=null)

{

System.out.println(str);

// get a next line

str = bufReader.readLine();

}

//close the file

bufReader.close();

}//End Main

} // end ReadFile

A:\>javac openfile.java

A:\>java ReadFile

Just For Today

Life is wonderful.

Who are you?

To be or not to be?

Wow! who am I?

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download