Files and I/O Streams in Java Written by Amir Kirsh, Edited by Liron Blecher © Amir Kirsh Agenda • System Parameters • File Class • I/O Streams • Reading from the standard input • Scanner Class • Binary files • Text files and character encoding System Parameters • Even though Java works on different platforms and is designed to be Platform Neutral, there might be cases when you need to get specific environment parameters • Use System.getProperties() to get all available properties • Use System.getProperty() to get a specific property (you must provide the property name) 3 examples.systemproperties DEMO 4 Agenda • System Parameters • File Class • I/O Streams • Reading from the standard input • Scanner Class • Binary files • Text files and character encoding File Class • The File class represents a file or directory • Supports inquiries on the file or directory, such as: • Rename • Open (if it’s a file) to get a stream of bytes for read / write • List all of its child files (if it’s a directory) • Delete it • etc. • Useful method to get all Root files (or directories) of a machine is the static method: File.listRoots() • In Java 7 – the entire File API was re-factored: http://docs.oracle.com/javase/tutorial/essential/io/index.html 6 Agenda • System Parameters • File Class • I/O Streams • Reading from the standard input • Scanner Class • Binary files • Text files and character encoding I/O Streams • In Java reading and writing data between different sources are all done using the same API – I/O Streams • I/O Stream classes represents a stream of bytes / chars • InputStream • InputStreamReader • BufferedReader • DataInputStream • I/O Streams are not related only to files, we can have a stream of bytes for network sockets, Byte Array or even for a String 8 I/O Streams • There are two major families of I/O Streams: • Input / Output streams – used for reading / writing one byte at a time from/to a resource • Reader / Writer – used to convert binary data to characters (and again, read/write them one character at a time) Reader and Writer streams wrap regular input/output streams • Buffered streams (used in both families) are used to read data (either bytes or characters) into an internal buffer. This makes them more efficient and also allows to read characters in lines (and not just one by one) 9 I/O Streams – Input / Output 10 I/O Streams – Reader / Writer 11 Streams - Closing • After finishing reading / writing from a stream (any stream), the stream needs to be closed (this is especially true for files, network, hardware, etc. • Use the close method to close the stream; since the method declared that it throws an IOException, it is usually called inside a try / catch block • In Java 7, there’s a shorter way to open a stream and close it using the try-with-resource statement http://java.dzone.com/articles/java-7-new-feature%E2%80%93 12 Streams Tip! When sending an existing object over and over again over an output stream, it might stay in memory on the other side (input stream). In order to be able to send the entire object again call the method reset before sending the object again. 13 examples.files.TextFile DEMO 14 Agenda • System Parameters • File Class • Streams • Reading from the standard input • Scanner Class • Binary files • Text files and character encoding Reading from the standard input try { BufferedReader in = new BufferedReader( new InputStreamReader(System.in) ); String str = ""; while (str != null) { str = in.readLine(); System.out.println(str.toUpperCase()); } } catch (IOException e) { } 16 Reading from the standard input • System.in an object of type InputStream • A stream of bytes (NOT chars!) • InputStreamReader • A bridge from byte stream to character stream, can read single chars • BufferedReader • adds the method “readLine” 17 Writing to the console • The method System.out.println() is a wrapper for this code: BufferedWriter consoleWriter = new BufferedWriter( new OutputStreamWriter(System.out)); 18 Agenda • System Parameters • File Class • I/O Streams • Reading from the standard input • Scanner Class • Binary files • Text files and character encoding Scanner Scanner is a helper class for getting input Scanner s = new Scanner(System.in); System.out.println("Please insert a string: "); String str = s.nextLine(); System.out.println("Please insert a number: "); int i = s.nextInt(); // may throw InputMismatchException 20 Scanner • The Scanner class is a wrapper for this code: • new BufferedReader(new InputStreamReader(System.in)); 21 Agenda • System Parameters • File Class • I/O Streams • Reading from the standard input • Scanner Class • Binary files • Text files and character encoding Binary Files • Binary Files hold data in binary format • DataInputStream / DataOutputStream -- for primitive types 23 examples.files DEMO 24 Serialization Serialization is a mechanism that enables to read/write entire objects over streams (to a file or a server socket). Using the marker interface Serializable interface and the ObjectInputStream / ObjectOutputStream to: • readObject() • writeObject() • When writing Object, you can set a version for a Class, thus enabling changing the class without the need to change the files it is written on: http://www.mkyong.com/java-bestpractices/understand-the-serialversionuid/ 25 Agenda • System Parameters • File Class • I/O Streams • Reading from the standard input • Scanner Class • Binary files • Text files and character encoding Text Files and Character Encoding Joel On SW about character encoding: http://www.joelonsoftware.com/articles/Unicode.html – a must read! • InputStream • An I/O stream of bytes (NOT chars!) • InputStreamReader • A bridge from byte stream to character stream, can read single chars • An important parameter is the Charset • Also when constructing a String out of bytes, it’s important to provide the Charset used on the byte array 27