lecture23

lecture23 - PIC 20A Lecture 23: Files and Streams Reading...

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

View Full Document Right Arrow Icon
PIC 20A Lecture 23: Files and Streams
Background image of page 1

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

View Full DocumentRight Arrow Icon
Reading Text Files 1. Create a FileReader 2. Pass the FileReader into a Scanner constructor 3. Use the Scanner to get the data from the file as you would if it were coming from the console ( System.in ) or from a String
Background image of page 2
Read Text Example FileReader reader = new FileReader("inputFile.txt"); Scanner in = new Scanner(reader); String firstLine = in.nextLine(); String nextWord = in.next(); int n = in.nextInt(); double x = in.nextDouble();
Background image of page 3

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

View Full DocumentRight Arrow Icon
Writing Text Files 1. Create a PrintWriter by passing in the target filename to the constructor 2. Use print or println methods to write numbers, String s, and Object s to the file 3. Close the PrintWriter using close() (usually in a finally block)
Background image of page 4
Write Text Example PrintWriter out = new PrintWriter("output.txt"); out.println("Data to save:"); out.println(1); out.print(new ClockTime()); out.close();
Background image of page 5

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

View Full DocumentRight Arrow Icon
• Creating a
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/12/2010 for the course PIC PIC 20A taught by Professor Keithouellette during the Spring '09 term at UCLA.

Page1 / 18

lecture23 - PIC 20A Lecture 23: Files and Streams Reading...

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

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