Java IO Basics PVGs College of Engineering Nashik
Java I/O Basics PVG's College of Engineering, Nashik
Overview • Java I/O – The java. io package – Streams, Readers and Writers • Files – Working with Files • URLs – Working with Internet Resources PVG's College of Engineering, Nashik
Java I/O – The Basics • Java I/O is based around the concept of a stream – Ordered sequence of information (bytes) coming from a source, or going to a ‘sink’ – Simplest stream reads/writes only a single byte, or an array of bytes at a time • Designed to be platform-independent • The stream concept is very generic – Can be applied to many different types of I/O – Files, Network, Memory, Processes, etc PVG's College of Engineering, Nashik
Java I/O – The Basics • The java. io package contains all of the I/O classes. – Many classes specialised for particular kinds of stream operations, e. g. file I/O • Reading/writing single bytes is quite limited – So, it includes classes which provide extra functionality – e. g. buffering, reading numbers and Strings (not bytes), etc. • Results in large inheritance hierarchy, with separate trees for input and output stream classes PVG's College of Engineering, Nashik
Java I/O -- Input. Stream PVG's College of Engineering, Nashik
Java I/O – Input. Streams PVG's College of Engineering, Nashik
Java I/O – Using Input. Streams • Basic pattern for I/O programming is as follows: Open a stream While there’s data to read Process the data Close the stream PVG's College of Engineering, Nashik
Java I/O – Using Input. Streams • I/O in Java: Input. Stream in = new File. Input. Stream(“c: \temp\myfile. txt”); int b = in. read(); //EOF is signalled by read() returning -1 while (b != -1) { //do something… b = in. read(); } in. close(); PVG's College of Engineering, Nashik
Java I/O – Using Input. Streams • But using buffering is more efficient, therefore we always nest our streams… Input. Stream inner = new File. Input. Stream(“c: \temp\myfile. txt”); Input. Stream in = new Buffered. Input. Stream(inner); int b = in. read(); //EOF is signalled by read() returning -1 while (b != -1) { //do something… b = in. read(); } in. close(); PVG's College of Engineering, Nashik
Java I/O – Using Input. Streams • We’ve omitted exception handling in the previous examples • Almost all methods on the I/O classes (including constructors) can throw an IOException or a subclass. • Always wrap I/O code in try…catch blocks to handle errors. PVG's College of Engineering, Nashik
Java I/O – Using Input. Streams Input. Stream in = null; try { Input. Stream inner = new File. Input. Stream(“c: \temp\myfile. txt”); in = new Buffered. Input. Stream(inner); //process file } catch (IOException e) { e. print. Stack. Trace(); } finally { try { in. close(); } catch (Exception e) {} } PVG's College of Engineering, Nashik
Java I/O – Output. Stream PVG's College of Engineering, Nashik
Java I/O – Output. Streams PVG's College of Engineering, Nashik
Java I/O – Using Input. Streams • Basic pattern for output is as follows: Open a stream While there’s data to write Write the data Close the stream PVG's College of Engineering, Nashik
Java I/O – Using Output. Streams • Output in Java: Output. Stream out = new File. Output. Stream(“c: \temp\myfile. txt”); while (…) { out. write(…); } out. close(); PVG's College of Engineering, Nashik
Java I/O – Using Output. Streams Output. Stream out = null; try { Output. Stream inner = new File. Output. Stream(“c: \temp\myfile. txt”); out = new Buffered. Output. Stream(inner); //write data to the file } catch (IOException e) { e. print. Stack. Trace(); } finally { try { out. close(); } catch (Exception e) {} } PVG's College of Engineering, Nashik
But That’s Not All! • Input/Output. Stream and sub-classes were part of Java 1. 1. • Java 1. 2 adds more classes specialised for character based I/O – The stream classes are for data I/O. • Classes for character I/O are called Readers and Writers • Why have specialised classes? – To support foreign languages PVG's College of Engineering, Nashik
Unicode • Each character in the ASCII character set fits into a single byte – …but that’s not enough for chinese, and other complex alphabets – Need more than a single byte – A Java character (char) is 2 bytes • Java handles text using Unicode – International standard character set, containing characters for almost all known languages – And a few imaginary ones! (Klingon, Elvish…) • Inside the JVM all text is held as Unicode PVG's College of Engineering, Nashik
Java Text I/O • Because byte != character for all languages, you have to turn bytes into chars using a Input/Output. Stream • Java provides Readers and Writers to save you this work. • These classes deal with streams of characters – Read/write single character or array of characters – Again there are classes specialised for particular purposes PVG's College of Engineering, Nashik
Java I/O – Reader PVG's College of Engineering, Nashik
Java I/O – Readers PVG's College of Engineering, Nashik
Using Readers Reader in = null; try { Reader inner = new File. Reader(“c: \temp\myfile. txt”); in = new Buffered. Reader(inner); //process file } catch (IOException e) { e. print. Stack. Trace(); } finally { try { in. close(); } catch (Exception e) {} } PVG's College of Engineering, Nashik
Java I/O – Writer PVG's College of Engineering, Nashik
Java I/O – Writers PVG's College of Engineering, Nashik
Using Writers Writer out = null; try { Writer inner = new File. Writer(“c: \temp\myfile. txt”); out = new Buffered. Writer(inner); //write data to the file } catch (IOException e) { e. print. Stack. Trace(); } finally { try { out. close(); } catch (Exception e) {} } PVG's College of Engineering, Nashik
- Slides: 25