1.Storing and recovering data:use a DataOutputStream to write the data and a DataInputStream to recover the data.If you use a DataOutputStream to write the data, then Java guarantees that you can accurately recover the data using a DataInputStream— regardless of what different platforms write and read the data. This is incredibly valuable, as anyone knows who has spent time worrying about platform-specific data issues.
2.UTF-8 is a multi-byte format, and the length of encoding varies according to the actual character set in use. If you’re working with ASCII or mostly ASCII characters (which occupy only seven bits), Unicode is a tremendous waste of space and/or bandwidth, so UTF-8 encodes ASCII characters in a single byte, and non-ASCII characters in two or three bytes. In addition, the length of the string is stored in the first two bytes of the UTF-8 string.However, writeUTF( ) and readUTF( ) use a special variation of UTF-8 for Java (which is completely described in the JDK documentation for those methods), so if you read a string written with writeUTF( ) using a non-Java program, you must write special code in order to read the string properly.
3.Since a double is always eight bytes long, to seek( ) to double number 5 you just multiply 5*8 to produce the seek value.RandomAccessFile is effectively separate from the rest of the I/O hierarchy, save for the fact that it implements the DataInput and DataOutput interfaces. It doesn’t support decoration, so you cannot combine it with any of the aspects of the InputStream and OutputStream subclasses.
4.The Java System class allows you to redirect the standard input, output, and error I/O streams using simple static method calls:
setIn(InputStream)
setOut(PrintStream)
setErr(PrintStream)
Redirecting output is especially useful if you suddenly start creating a large amount of output on your screen, and it’s scrolling past faster than you can read it.4 Redirecting input is valuable for a command-line program in which you want to test a particular user-input sequence repeatedly.
5.I/O redirection manipulates streams of bytes, not streams of characters; thus, InputStreams and OutputStreams are used rather than Readers and Writers.
6.The Java "new" I/O library, introduced in JDK 1.4 in the java.nio.* packages, has one goal: speed.In fact, the "old" I/O packages have been reimplemented using nio in order to take advantage of this speed increase, so you will benefit even if you don’t explicitly write code with nio.The speed comes from using structures that are closer to the operating system’s way of performing I/O: channels and buffers.
7.Three of the classes in the "old" I/O have been modified so that they produce a FileChannel: FileInputStream, FileOutputStream, and, for both reading and writing, RandomAccessFile. Notice that these are the byte manipulation streams, in keeping with the low-level nature of nio. The Reader and Writer character-mode classes do not produce channels, but the java.nio.channels.Channels class has utility methods to produce Readers and Writers from channels.
I/O redirecting:
import java.io.*; public class Redirecting { public static void main(String[] args) throws IOException { PrintStream console = System.out; BufferedInputStream in = new BufferedInputStream( new FileInputStream("Redirecting.java")); PrintStream out = new PrintStream( new BufferedOutputStream( new FileOutputStream("test.out"))); System.setIn(in); System.setOut(out); System.setErr(out); BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); String s; while((s = br.readLine()) != null) System.out.println(s); out.close(); // Remember this! System.setOut(console); } }
Exercise 21: (1) Write a program that takes standard input and capitalizes all characters, then puts the results on standard output. Redirect the contents of a file into this program (the process of redirection will vary depending on your operating system).
import java.io.*; public class Echo { public static void main (String[] args) throws IOException{ BufferedInputStream br = new BufferedInputStream( new FileInputStream("src//Echo.java")); System.setIn(br); BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); String s; while((s=stdin.readLine())!=null&& s.length()!=0) System.out.println(s.toUpperCase()); } }
Exercise 22: (5) Modify OSExecute.java so that, instead of printing the standard output stream, it returns the results of executing the program as a List of Strings. Demonstrate the use of this new version of the utility.
import java.io.*; import java.util.*; public class OSExecute { public static void command(String command) { ArrayList<String> temp = new ArrayList<String>(); boolean err = false; try { Process process = new ProcessBuilder(command.split(" ")).start(); BufferedReader results = new BufferedReader(new InputStreamReader( process.getInputStream())); String s; while ((s = results.readLine()) != null) temp.add(s);/// BufferedReader errors = new BufferedReader(new InputStreamReader( process.getErrorStream())); while ((s = errors.readLine()) != null) { temp.add(s);//// err = true; } } catch (Exception e) { // Compensate for Windows 2000, which throws an // exception for the default command line: if (!command.startsWith("CMD /C")) command("CMD /C " + command); else throw new RuntimeException(e); } if (err) throw new OSExecuteException("Errors executing " + command); //////// while(temp.remove("")); temp.trimToSize(); System.out.println(temp.toString()); //////// } } // /:~