com s 207 file instructor: ying cai department of computer science iowa state university

11
COM S 207 File Instructor: Ying Cai Department of Computer Science Iowa State University [email protected]

Upload: frank-robbins

Post on 19-Jan-2018

212 views

Category:

Documents


0 download

DESCRIPTION

A few basic commands Windows (under DOS console) cd (change directory)  cd.. (go to the parent directory)  cd src (go to “src” directory) dir (list all files/directories in the current directory) type data.txt (display the content of data.txt) Unix/Max/Linux (under terminal) cd (change directory)  cd.. (go to the parent directory)  cd src (go to “src” directory) ls (list all files/directories in the current directory) more data.txt (display the content of data.txt)

TRANSCRIPT

Page 1: COM S 207 File Instructor: Ying Cai Department of Computer Science Iowa State University

COM S 207File

Instructor: Ying Cai

Department of Computer ScienceIowa State [email protected]

Page 2: COM S 207 File Instructor: Ying Cai Department of Computer Science Iowa State University

Files in your computerThey are organized into a tree directory

Windows/DOS MAC/UNIX/Terminal

Page 3: COM S 207 File Instructor: Ying Cai Department of Computer Science Iowa State University

A few basic commandsWindows (under DOS console) cd (change directory)

cd .. (go to the parent directory) cd src (go to “src” directory)

dir (list all files/directories in the current directory)

type data.txt (display the content of data.txt)Unix/Max/Linux (under terminal) cd (change directory)

cd .. (go to the parent directory) cd src (go to “src” directory)

ls (list all files/directories in the current directory) more data.txt (display the content of data.txt)

Page 4: COM S 207 File Instructor: Ying Cai Department of Computer Science Iowa State University

Reading and Writing Text Files

Text files – files containing simple text Created with editors such as notepad, html,

etc.

Simplest way to learn it so extend our use of Scanner Associate with files instead of System.in

All input classes, except Scanner, are in java.io import java.io.*;

Page 5: COM S 207 File Instructor: Ying Cai Department of Computer Science Iowa State University

File Classjava.io.File associated with an actual file on hard drive used to check file's status

Constructors File(<full path>) File(<path>, <filename>)

Methods exists() canRead(), canWrite() isFile(), isDirectory()

Page 6: COM S 207 File Instructor: Ying Cai Department of Computer Science Iowa State University

Reading FilesYou can use File Has an exists() method we can call to

avoid FileNotFoundException

File file = new File ("input.txt"); Scanner fin;if(file.exists()){fin = new Scanner(file);

} else {//ask for another file

}

Page 7: COM S 207 File Instructor: Ying Cai Department of Computer Science Iowa State University

Reading FilesOnce we have a Scanner, we can use methods we already know: next, nextLine, nextInt, etc.

Reads the information from the file instead of console

Page 8: COM S 207 File Instructor: Ying Cai Department of Computer Science Iowa State University

Closing a FileOnly main difference is that we have to close the file stream when we are done writing

If we do not, not all output will written

At the end of output, call close()

fout.close();

Page 9: COM S 207 File Instructor: Ying Cai Department of Computer Science Iowa State University

Closing a FileWhy? When you call print() and/or println(),

the output is actually written to a buffer. When you close or flush the output, the buffer is written to the file

The slowest part of the computer is hard drive operations – much more efficient to write once instead of writing repeated times

Page 10: COM S 207 File Instructor: Ying Cai Department of Computer Science Iowa State University

File LocationsWhen determining a file name, the default is to place in the same directory as your .class filesIf we want to define other place, use an absolute path (e.g. c:\My Documents)

in = new FileReader(“c:\\homework\\input.dat”);

Why \\ ?

Page 11: COM S 207 File Instructor: Ying Cai Department of Computer Science Iowa State University

Sample ProgramTwo things to notice: Have to import from java.io I/O requires us to catch checked

exceptionsjava.io.IOException