L11-file-Read-Write

L11-file-Read-Write - CSE205 Concepts of Computer Science...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE205 Concepts of Computer Science and Data Structures
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
1-2 FileReader and BufferedReader FileReader is used for input of character data from a disk file. The input file can be an ordinary ASCII, one byte per character text file. A Reader stream automatically translates the characters from the disk file format into the internal char format. As with output, it is good practice to use a buffer to improve efficiency. Use BufferedReader for this. This is the same class we've been using for keyboard input. These lines should look familiar: BufferedReader stdin = new BufferedReader(new InputStreamReader( System.in ));
Background image of page 2
1-3 Reading/Writing Text Files Reading Text Files: Instead of reading from the standard input, we can read from text files using the classes FileReader and BufferedReader. import java.io.*; public class ReadingFromFile { public static void main (String[] args) { String line, file = “input.txt"; try { FileReader fr = new FileReader (file); BufferedReader inFile = new BufferedReader (fr); //Scanner inFile = new Scanner(fr); instead of the above // line = inFile.nextLine(); line = inFile.readLine(); while (line != null) { System.out.println(line); line = inFile.readLine(); } inFile.close(); } catch (FileNotFoundException exception) { System.out.println ("The file " + file + " was not found."); }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/18/2011 for the course CSE 205 taught by Professor Matasu during the Spring '08 term at ASU.

Page1 / 8

L11-file-Read-Write - CSE205 Concepts of Computer Science...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online