{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

file-io - the new data are written into it • If file...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Reading and Writing Text Files Reading Text Files Simplest way to read text: use Scanner class To read from a disk file, construct a FileReader Then, use the FileReader to construct a Scanner object Use Scanner methods to read data from file – next , nextLine , nextInt , and nextDouble FileReader reader = new FileReader("input.txt"); Scanner in = new Scanner(reader); Writing Text Files To write to a file, construct a PrintWriter object If file already exists, it is emptied before
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: the new data are written into it • If file doesn't exist, an empty file is created PrintWriter out = new PrintWriter("output.txt"); Writing Text Files • Use print and println to write into a PrintWriter : • You must close a file when you are done processing it: • Otherwise, not all of the output may be written to the disk file out.println(29.95); out.println(new Rectangle(5, 10, 15, 25)); out.println("Hello, World!"); out.close();...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online