01.0.fileio.gray

01.0.fileio.gray - Sending Output to a (Text) File File I/O...

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

View Full Document Right Arrow Icon
CSE 201 – Elementary Computer Programming 1 File I/O Exception Handling CSE 201 – Elementary Computer Programming 2 Sending Output to a (Text) File import java.util.Scanner; import java.io.*; public class TextFileOutputDemo1 { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); System.out.print(“Enter your name: ”); String name = in.nextLine(); PrintWriter outputFile = new PrintWriter(new FileWriter(“out.txt”)); outputFile.println(name); outputFile.close(); } } CSE 201 – Elementary Computer Programming 3 Sending Output to a (Text) File import java.util.Scanner; import java.io.*; public static void main(String[] args) throws IOException Scanner in = new Scanner(System.in); System.out.print(“Enter your name: ”); String name = in.nextLine(); PrintWriter outputFile = new PrintWriter(new FileWriter(“out.txt”)); outputFile.println(name); outputFile.close(); } } Java API Description of every function provided by Java. http://java.sun.com/j2se/1.5.0/docs/api/ CSE 201 – Elementary Computer Programming 4 Sending Output to a (Text) File import java.util.Scanner; import java.io.*; public class TextFileOutputDemo1 { public static void main(String[] args) throws IOException { String name = in.nextLine(); PrintWriter outputFile = new PrintWriter(new FileWriter(“out.txt”)); outputFile.println(name); outputFile.close(); } } IO Exceptions Exception Handling is discussed in Chapter 12, Section 1 CSE 201 – Elementary Computer Programming 5 Sending Output to a (Text) File import java.util.Scanner; { public static void main(String[] args) throws IOException { System.out.print(“Enter your name: ”); String name = in.nextLine(); PrintWriter outputFile = new PrintWriter(new FileWriter(“out.txt”)); outputFile.println(name); outputFile.close(); } } Using Files File I/O is discussed in Chapter 4, Section 10 CSE 201 – Elementary Computer Programming 6 Creating a File for Text Output ± We need a couple of new classes: ± PrintWriter ± FileWriter ± To gain access to them we need to import them with: ± import java.io.*; ± To create and open the file: ± PrintWriter outFile = new PrintWriter( new FileWriter(fileName)); String
Background image of page 1

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

View Full DocumentRight Arrow Icon
CSE 201 – Elementary Computer Programming 7 Writing Output to a Text File ± The PrintWriter class has output methods you are already familiar with: ± print ± println ± Instead of calling System.out.print(. ..) or System.out.println(. ..), you invoke: ± outFile.print(. ..) ± outFile.println(. ..) CSE 201 – Elementary Computer Programming 8 Closing the File ± It is important to remember to explicitly close a file you are done writing to: ± outFile.close() Pg. 239, Checkpoint 4.18 4.19 CSE 201 – Elementary Computer Programming 9 Your Turn ± Write a program that asks the user for a
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/07/2009 for the course CSE 201 taught by Professor All during the Spring '09 term at Ohio State.

Page1 / 6

01.0.fileio.gray - Sending Output to a (Text) File File I/O...

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

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