Example 16.1 Using the File class Objective: Write a program that demonstrates how to create files in a platform-independent way and use the methods in the file class to obtain their properties. Figure 16. 1 shows a sample run of the program on Windows, and Figure 16.2 a sample run on Unix Command Prompt COMmand Prompt-telnet panda :\book >java TestFileclass oes it exist? true home /liang/bool an it be read? true java TestFilecla an it be written? true Does it s it a directory false an it be read? true Is it a file? tr an it be written? true Is it absolute? false it a directory? false Is it hidden? false s it a file? tr hat s it absolute? false s ath? C: \book\\image\ hat is its canonical path? C: \book\image\us. gif what is its absolute path?/home/liang/book/, /image/us. gif nical is its nam hen was it last modified? Sat May 08 14: 00: 34 EDT 1999 at is its path? ./ lmage /us, a1 What is the path separator? Then was it last modi fied? Wed Jan 23 11: 00: 14 EST 2002 hat is the name separator? What is the path separator? hat is the name separator? C: \book> Liang, Introduction to Java Programming TestFileclass Run
Liang,Introduction to Java Programming,revised by Dai-kaiyu 6 Example 16.1 Using the File Class TestFileClass Run Objective: Write a program that demonstrates how to create files in a platform-independent way and use the methods in the File class to obtain their properties. Figure 16.1 shows a sample run of the program on Windows, and Figure 16.2 a sample run on Unix
How is io handled in java? A File object encapsulates the properties of a file or a path, but does not contain the methods for reading/writing data from/to a file. In order to perform 1/O, you need to create objects using appropriate Java 1/O classes FileReader input new FileReader("temp. txt)i int code input read()i system. out. println((char)code. O Return the unicode of the created from an input class reated from an File output class Output stream FileWriter output new FileWriter("temp. txt")i output. write("Java 101)i output. close()i Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 7 How is I/O Handled in Java? A File object encapsulates the properties of a file or a path, but does not contain the methods for reading/writing data from/to a file. In order to perform I/O, you need to create objects using appropriate Java I/O classes. Program Input object created from an input class Output object created from an output class Input stream Output stream File File FileWriter output = new FileWriter("temp.txt"); output.write("Java 101"); output.close(); FileReader input = new FileReader("temp.txt"); int code = input.read(); System.out.println((char)code); Return the unicode of the char
Coding essentials Declaring exception in the method Using try-catch block public static void main(String [ args) public static void main(String[] args) throws工 EXception Filewriter output FileWriter output new FileWriter(temp. txt)i new FileWriter("temp. txt)i output. write("Java 101) output. write("Java 101 )i tput close()i output. close()i FileReader input FileReader input new FileReader(temp. txt) new FileReader(temp. txt)i nt cod input. read() int code input read()i System. out. printin((char) code)i System. out. println((char)code)i put close() catch (IOException ex) t ex printstackTrace()i Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 8 Coding Essentials public static void main(String[] args) throws IOException { FileWriter output = new FileWriter("temp.txt"); output.write("Java 101"); output.close(); FileReader input = new FileReader("temp.txt"); int code = input.read(); System.out.println((char)code); input.close(); } Declaring exception in the method public static void main(String[] args) { try { FileWriter output = new FileWriter("temp.txt"); output.write("Java 101"); output.close(); FileReader input = new FileReader("temp.txt"); int code = input.read(); System.out.println((char)code); input.close(); } catch (IOException ex) { ex.printStackTrace(); } } Using try-catch block
Text File vs. Binary File o Data stored in a text file are represented in human-readable form Data stored in a binary file are represented in binary form You cannot read binary files. binary files are designed to be read by programs. For example, the Java source programs are stored in text files and can be read by a text editor but the Java classes are stored in binary files and are read by the jVm. The advantage of binary files is that they are more efficient to process than text files. o you can imagine that a text file consists ofa sequence of characters and a binary file consists of a sequence of bits. For example, the decimal integer 199 is stored as the sequence of three characters: 19,9 in a text file and the same integer is stored as a byte-type value cl in a binary file, because decimal 199 equals to hex C7 Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 9 Text File vs. Binary File ⚫ Data stored in a text file are represented in human-readable form. Data stored in a binary file are represented in binary form. You cannot read binary files. Binary files are designed to be read by programs. For example, the Java source programs are stored in text files and can be read by a text editor, but the Java classes are stored in binary files and are read by the JVM. The advantage of binary files is that they are more efficient to process than text files. ⚫ you can imagine that a text file consists of a sequence of characters and a binary file consists of a sequence of bits. For example, the decimal integer 199 is stored as the sequence of three characters: '1', '9', '9' in a text file and the same integer is stored as a byte-type value C7 in a binary file, because decimal 199 equals to hex C7
Text l/o classes Inputstream Reader FileReader Reader Buffered Reader Object Buffered write Writer OutputStream Writer File writer Print writer Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 10 Text I/O Classes Reader Writer Object PrintWriter BufferedWriter FileReader FileWriter InputStreamReader BufferedReader OutputStreamWriter