1 of 11

File Input and Output

modified 2/22/2018

2 of 11

3 Ways to Read a Text File in Java

  1. FileReader
  2. BufferedReader
  3. Scanner
  4. Each has pros and cons
    1. FileReader
      1. Convenience class for reading character files.
    2. BufferedReader allows for fast reading
      • It does buffering for efficient reading of characters, arrays, and lines.
    3. Scanner allows for parsing

3 of 11

File Class

  • The File class provides methods for dealing with files or directories.
  • InputStream and OutputStream are used to read and write 8-bit quantities and process binary files.
  • Reader and Writer that are used to read and write 16-bit Unicode character values and process text files.

4 of 11

1. FileReader

import java.io.*;�public class ReadingFromFile�{� public static void main(String[] args) throws Exception� {� // pass the path to the file as a parameter� FileReader fr = new FileReader("C:\\Users\\pankaj\\Desktop\\test.txt");� // fully qualified address

int i;� while ((i=fr.read()) != -1)� System.out.print((char) i);� }�}

5 of 11

2. Buffered Reader

import java.io.*;

public class ReadFromFile2

{

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

{

// We need to provide file path as the parameter:

// double backquote is to avoid compiler interpret words

// like \test as \t (ie. as a escape sequence)

File file = new File("C:\\Users\\pankaj\\Desktop\\test.txt");

// fully qualified address

BufferedReader br = new BufferedReader(new FileReader(file));

String st;

while ((st = br.readLine()) != null)

System.out.println(st);

}

}

Exception handling needed

6 of 11

3. Scanner

import java.io.File;

import java.util.Scanner;

public class ReadFromFileUsingScanner

{

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

{

// pass the path to the file as a parameter

File file = new File("C:\\Users\\pankaj\\Desktop\\test.txt");

Scanner sc = new Scanner(file);

while (sc.hasNextLine())

System.out.println(sc.nextLine());

}

}

7 of 11

Reading From a Text File - Example

import java.util.Scanner;�import java.io.*;�public class ReadTextFile�{� public static void main (String [] args) throws IOException � {� File inFile = new File ("input.txt");� Scanner sc = new Scanner (inFile);� while (sc.hasNextLine())� {� String line = sc.nextLine();� System.out.println (line);� }� sc.close();� } }�

8 of 11

Writing to a Text File - Example

import java.io.*;�public class WriteTextFile�{� public static void main (String [ ] args) throws IOException� {� File outFile = new File ("output.txt");� FileWriter fWriter = new FileWriter (outFile);� PrintWriter pWriter = new PrintWriter (fWriter);� pWriter.println ("This is a line.");� pWriter.println ("This is another line.");� pWriter.close();� }�}

9 of 11

Import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

public class Prob01 {

private static final String INPUT_FILE_NAME = "Prob01.in.txt";

public static void main(String[] args) {

try {

// prepare to read the file

File inFile = new File(INPUT_FILE_NAME);

FileReader fr = new FileReader(inFile);

BufferedReader br = new BufferedReader(fr);

String inLine = null;

// get the number of test cases

int T = Integer.parseInt(br.readLine());

// loop through test cases

for (int t=0; t<T; t++) {

// read the line of text

inLine = br.readLine();

// print it twice

System.out.println(inLine);

System.out.println(inLine);

}

// clean up

br.close();

fr.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

10 of 11

Resources

"File Input and Output" www.cs.utexas.edu/~mitra/csSummer2009/cs303/lectures/fileIO.html

"Different ways of Reading a text file in Java" Geeks for Geeks a computer science portal for geeks www.geeksforgeeks.org/different-ways-reading-text-file-java/

"Java.io.File class in Java", Geeks for Geeks a computer science portal for geeks www.geeksforgeeks.org/file-class-in-java/

"File Handling in Java using FileWriter and FileReader" Geeks for Geeks a computer science portal for geeks www.geeksforgeeks.org/file-handling-java-using-filewriter-filereader/

"Reading, Writing, and Creating Files" Java Tutorials. Oracle Java Documentation docs.oracle.com/javase/tutorial/essential/io/file.html

11 of 11

Resources

Purcell, John. “Java File: Reading and Writing Files in Java.” Cave of Programming, www.caveofprogramming.com/java/java-file-reading-and-writing-files-in-java.html. Accessed 20 February 2020.