Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tim Wentz Nathaniel Smith Andrew Johnson. Files in Java Create a new file handle using: ▫new File(String pathname); Pathnames can be either relative or.

Similar presentations


Presentation on theme: "Tim Wentz Nathaniel Smith Andrew Johnson. Files in Java Create a new file handle using: ▫new File(String pathname); Pathnames can be either relative or."— Presentation transcript:

1 Tim Wentz Nathaniel Smith Andrew Johnson

2 Files in Java Create a new file handle using: ▫new File(String pathname); Pathnames can be either relative or absolute. Pathnames are system dependant ▫Ex. The place where my documents are stored: Unix: /home/user/ ▫Windows: C:\Documents and Settings\user\ There isn’t much we can do to the contents of a File until we create some type of input stream object.

3 Java I/O Package Java input and output methods are contained in the java.io.* package library, which contains methods to read and write to terminals, files, and internet sockets. There’s no such thing as a stream open for both input and output. File handling is a bit susceptible to error so Java returns a syntax error when certain methods are not surrounded by a try/catch clause. It’s also important to close a file after use, otherwise the opened file might not be accessible later. The following are common classes imported to write or read a file ▫FileReader > InputStreamReader > Reader read streams of characters ▫BufferedReader > Reader reads text from a character-input stream ▫FileWriter > OutputStreamWriter > Writer write streams of characters ▫BufferedWriter > Writer writes text from a character-output stream

4 Reading There are various ways to read a file, all of which require constructing a type of input stream object and calling a type of read method. ▫new Reader(File a) reads a character using read() ▫new BufferedReader(Reader a) reads a line of characters at a time with the readLine() method ▫new FileInputStream(String fileName) returns a byte using read() NOTE: Java actually returns the byte as an int.

5 Writing Writing to a file works pretty much the same way as reading. ▫new Writer(File a) writes a character with write(char val) ▫new BufferedWriter(Writer a) writes a String with write(String text). Also newLine() can be called to insert a line in the file. ▫new FileOutputStream(String fileName) writes a byte using write(int aByte)

6 File I/O Template The code below is a very basic example of how reading from a file works in practice. try {// required to surround a called file with try/catch clause // we create the Readable object referencing the file we wish to read from FileInputStream file = new FileInputStream(“filename.elo"); // iterate through the bytes in the file and output them to the console while (int input = file.read(); input != -1; input = file.read()) System.out.print(input + " "); file.close();// always close a file after use } catch (IOException e) { System.out.println("Err: " + e.toString()); }

7 IO Exceptions If a path or file does not exist we’d get a IO exception error, so we need to surround the call file method with a try/catch clause. The path could cause an error because the program was made with only a certain OS in mind, or the path/file name could have been entered incorrectly by someone. Some common exceptions ▫IOException Signals that an I/O exception of some sort has occurred. ▫FileNotFoundException Signals that an attempt to open the file denoted by a specified pathname has failed. ▫EOFException Signals that an end of file or end of stream has been reached unexpectedly during input.

8 File I/O Demo Open FileIODemo in the repository Implement the exportToFile(File a) method in the FileIOPanel class. The result should be the actual script of your masterpiece saved when you save.

9 FIN


Download ppt "Tim Wentz Nathaniel Smith Andrew Johnson. Files in Java Create a new file handle using: ▫new File(String pathname); Pathnames can be either relative or."

Similar presentations


Ads by Google