new InputStreamReader(System.in); • An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset • InputStreamReader has methods for reading one char at a time • We don’t want to read one char at a time from user!
new BufferedReader(new InputStreamReader(System.in)); – String mj = a.readLine(); • Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
void main(String [] args) throws Exception { FileInputStream f = new FileInputStream("Main.java"); // Bridge from byte to char InputStreamReader input = new InputStreamReader(f, StandardCharsets.UTF_8); char oneChar = (char) input.read(); System.out.println(oneChar); input.close(); } } By using InputStreamReader/ Writer you can define the encoding! Use OutputStreamWriter for writing
upon exiting the try block (like finally) • The resource object must implement AutoCloseable interface • The interface has only one method: close • If closing causes exception, it’s suppressed (ignore). Possible to get it using getSuppressed() method
updated • Called NIO.2 revision • New classes (java.nio): – Path – Locate a file in a file system • Paths – Convert a URI to Path object – Files – Operate on files, directories and other types of files – FileVisitor – Traversing files in a tree – WatchService – File change modification API
file system. • Supporting API to java.io.File • File to Path: – File f = new File(”/foo/bar/file.txt”); – Path p = f.toPath(); • Path to File – File f2 = p.toFile(); • Path is an interface! Instantiating using either File or or Paths class – Path p = Paths.get(“file.txt”);
– Object class must implement serializable marker interface – Read and write using ObjectInputStream and ObjectOutputStream • Serialization is used in Java RMI
new FileOutputStream("car.dat"); oos = new ObjectOutputStream(fos); oos.writeObject(datsun); // Read the object fis = new FileInputStream("car.dat"); ois = new ObjectInputStream(fis); Car datsun2 = (Car) ois.readObject();
disk.. except attribute is marked with transient keyword • Mark attributes to transient when the information is secret or uneccessary. • When object is deserializaled, transient attributes values are null
needs. When there is a need for higher performance, use Java NIO (New I/O) (java.nio) • Less GC, less threads, more efficient use of operating system • Provides scalable I/O operations on both binary and character files. Also a simple parsing facility based on regular expressions • A little bit harder to use than java.io
bytes one at a time. • java.nio » Block: movement of many bytes (blocks) at a time • Processing data by block can be much faster than one byte at a time
java.io • All data transferred in java.nio must go through a Channel • Buffer is a container object, before sending data into a channel, the data must be wrapped inside a Buffer • Buffer is an object, which holds an array of bytes
Get a channel via the FileInputStream FileChannel fc = fin.getChannel(); // Create a buffer ByteBuffer buffer = ByteBuffer.allocate( 1024 ); // Read from channel into a buffer fc.read( buffer );
Get a channel via the FileOutputStream FileChannel fc = fout.getChannel(); // Create a buffer ByteBuffer buffer = ByteBuffer.allocate( 1024 ); // Data to be saved byte [] message = "this will be saved".toByteArray(); // Write into buffer for ( int i=0; i<message.length; i++ ) { buffer.put( message[i] ); } // Flip the buffer, this will be explained later buffer.flip(); // Writes SOME bytes from the buffer! fc.write( buffer );
• These three variables track the state of the buffer • position: is the index of the next element to be read or written. A buffer's position is never negative and is never greater than its limit. • limit: is the index of the first element that should not be read or written. A buffer's limit is never negative and is never greater than its capacity • capacity: is the number of elements buffer contains. The capacity of a buffer is never negative and never changes.