Return to Snippet

Revision: 34046
at October 16, 2010 12:06 by trusktr


Updated Code
import java.io.*

/**
 * This program reads a text file line by line and prints them to the console. It uses
 * FileReader and BufferedReader to read the file.
 * 
 */
public class FileInput {

	public static void main(String[] args) {
		File file = new File("MyFile.txt");
		FileReader fr = null;
		BufferedReader br = null;
		try {
			fr = new FileReader(file);
			br = new BufferedReader(fr);
			while (br.ready()) { // br.read() returns false when the Buffer is not ready (end of file).
				System.out.println(br.readLine()); // this statement reads a line from the file and prints it to the console (and moves to next line and is "ready" if there IS a next line).
			}
			br.close(); // dispose of the resources after using them.
		}
		catch (FileNotFoundException e) { // File not found.
			e.printStackTrace();
			// Put something here like an error message.
			System.out.println("The file could not be found.");
		}
		catch (IOException e) { // Error with transaction.
			e.printStackTrace();
			// Put something here like an error message.
			System.out.println("There was an error reading the file.");
		}
	}
}

Revision: 34045
at October 16, 2010 12:04 by trusktr


Updated Code
import java.io.*

/**
 * This program reads a text file line by line and print to the console. It uses
 * FileOutputStream to read the file.
 * 
 */
public class FileInput {

	public static void main(String[] args) {
		File file = new File("MyFile.txt");
		FileReader fr = null;
		BufferedReader br = null;
		try {
			fr = new FileReader(file);
			br = new BufferedReader(fr);
			while (br.ready()) { // br.read() returns false when the Buffer is not ready (end of file).
				System.out.println(br.readLine()); // this statement reads a line from the file and prints it to the console (and moves to next line and is "ready" if there IS a next line).
			}
			br.close(); // dispose of the resources after using them.
		}
		catch (FileNotFoundException e) { // File not found.
			e.printStackTrace();
			// Put something here like an error message.
			System.out.println("The file could not be found.");
		}
		catch (IOException e) { // Error with transaction.
			e.printStackTrace();
			// Put something here like an error message.
			System.out.println("There was an error reading the file.");
		}
	}
}

Revision: 34044
at October 16, 2010 12:03 by trusktr


Updated Code
import java.io.*

/**
 * This program reads a text file line by line and print to the console. It uses
 * FileOutputStream to read the file.
 * 
 */
public class FileInput {

	public static void main(String[] args) {
		File file = new File("MyFile.txt");
		FileReader fr = null;
		BufferedReader br = null;
		try {
		  fr = new FileReader(file);
		  br = new BufferedReader(fr);
		  while (br.ready()) { // br.read() returns false when the Buffer is not ready (end of file).
			System.out.println(br.readLine()); // this statement reads a line from the file and prints it to the console (and moves to next line and is "ready" if there IS a next line).
		  }
		  br.close(); // dispose of the resources after using them.
		}
		catch (FileNotFoundException e) { // File not found.
		  e.printStackTrace();
		  // Put something here like an error message.
		  System.out.println("The file could not be found.");
		}
		catch (IOException e) { // Error with transaction.
		  e.printStackTrace();
		  // Put something here like an error message.
		  System.out.println("There was an error reading the file.");
		}
	}
}

Revision: 34043
at October 16, 2010 11:50 by trusktr


Updated Code
import java.io.*

/**
 * This program reads a text file line by line and print to the console. It uses
 * FileOutputStream to read the file.
 * 
 */
public class FileInput {

	public static void main(String[] args) {
		File file = new File("MyFile.txt");
		FileReader fr = null;
		BufferedReader br = null;

		try {
			fr = new FileReader(file);
			br = new BufferedReader(fr);

			// br.read() returns false if when the Buffer is not ready (reached end of file).
			while (br.ready()) {

			// this statement reads a line from the file and prints it to the console (and moves to next line and is "ready" if there IS any following bytes).
			System.out.println(br.readLine());
			}

			// dispose of the resources after using them.
			br.close();

		}
		catch (FileNotFoundException e) {
			e.printStackTrace(); // File not found.

			// Put something here like an error message.
			System.out.println("The file could not be found.");
		}
		catch (IOException e) {
			e.printStackTrace(); // Error with transaction.

			// Put something here like an error message.
			System.out.println("There was an error reading the file.");
		}
	}
}

Revision: 34042
at October 16, 2010 09:07 by trusktr


Updated Code
import java.io.*

/**
 * This program reads a text file line by line and print to the console. It uses
 * FileOutputStream to read the file.
 * 
 */
public class FileInput {

  public static void main(String[] args) {

    File file = new File("C:\\MyFile.txt");
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;

    try {
      fis = new FileInputStream(file);

      // Here BufferedInputStream is added for fast reading.
      bis = new BufferedInputStream(fis);
      dis = new DataInputStream(bis);

      // dis.available() returns 0 if the file does not have more lines.
      while (dis.available() != 0) {

      // this statement reads the line from the file and print it to
        // the console.
        System.out.println(dis.readLine());
      }

      // dispose all the resources after using them.
      fis.close();
      bis.close();
      dis.close();

    } catch (FileNotFoundException e) {
      e.printStackTrace(); // File not found.
    } catch (IOException e) {
      e.printStackTrace(); // Error with transaction.
    }
  }
}

Revision: 34041
at October 16, 2010 08:56 by trusktr


Updated Code
// Use: import java.io.*

try {
	BufferedReader in = new BufferedReader(new FileReader("infilename"));
	String str;
	while ((str = in.readLine()) != null) {
		process(str);
	}
	in.close();
}
catch (IOException e) {
	// Put something here like an error message or an action to do if file transaction fails.
}

Revision: 34040
at October 16, 2010 08:45 by trusktr


Initial Code
try {
	BufferedReader in = new BufferedReader(new FileReader("infilename"));
	String str;
	while ((str = in.readLine()) != null) {
		process(str);
	}
	in.close();
}
catch (IOException e) {
	// Put something here like an error message or an action to do if file transaction fails.
}

Initial URL


Initial Description
Use this to read from a file. It is wrapped in a try/catch statement in case something breaks when trying to read the file. It will simply throw and exception (error) and continue rather than crashing your app.

Initial Title
How to read from a file in Java

Initial Tags
java, files

Initial Language
Java