【正文】
ndline program in which you want to test a particular userinput sequence repeatedlyNew IO The Java new IO library introduced in JDK 14 in the javanio packages has one goal speed In fact the old IO packages have been reimplemented using nio in order to take advantage of this speed increase so you will benefit even if you dont explicitly write code with nio The speed increase occurs both in file IO which is explored here and in network IO which is covered in Thinking in Enterprise Java The speed es from using structures that are closer to the operating systems way of performing IO channels and buffers You could think of it as a coal mine the channel is the mine containing the seam of coal the data and the buffer is the cart that you send into the mine The cart es back full of coal and you get the coal from the cart That is you dont interact directly with the channel you interact with the buffer and send the buffer into the channel The channel either pulls data from the buffer or puts data into the buffer The only kind of buffer that municates directly with a channel is a ByteBufferthat is a buffer that holds raw bytes If you look at the JDK documentation for Buffer youll see that its fairly basic You create one by telling it how much storage to allocate and there are methods to put and get data in either raw byte form or as primitive data types But theres no way to put or get an object or even a String Its fairly lowlevel precisely because this makes a more efficient mapping with most operating systems Three of the classes in the old IO 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 lowlevel nature of nio The Reader and Writer charactermode classes do not produce channels but the nelsChannels class has utility methods to produce Readers and Writers from channelsReading from an InputStream with FilterlnputStream The FilterlnputStream classes acplish two significantly different things DatalnputStream allows you to read different types of primitive data as well as String objects All the methods start with read such as readByte readFloat etc This along with its panion DataOutputStream allows you to move primitive data from one place to another via a stream The remaining FilterlnputStream classes modify the way an InputStream behaves internally whether its buffered or unbuffered whether it keeps track of the lines its reading allowing you to ask for line numbers or set the line number and whether you can push back a single character The last two classes look a lot like support for building a piler they were probably added to support the experiment of building a Java piler in Java so you probably wont use them in general programming Youll need to buffer your input almost every time regardless of the IO device youre connecting to so it would have made more sense for the IO library to have a special case or simply a method call for unbuffered input rather than buffered inputWriting to an OutputStream with FilterOutputStream The plement to DatalnputStream is DataOutputStream which formats each of the primitive types and String objects onto a stream in such a way that any DatalnputStream on any machine can read them All the methods start with write such as writeByte writeFloat etc The original intent of PrintStream was to print all of the primitive data types and String objects in a viewable format This is different from DataOutputStream whose goal is to put data elements on a stream in a way that DatalnputStream can portably reconstruct them The two important methods in PrintStream are print and println which are overloaded to print all the various types The difference between print and println is that the latter adds a newline when its done BufferedOutputStream is a modifier and tells the stream to use buffering so you dont get a physical write every time you write to the stream Off by itself RandomAccessFileRandomAccessFile is used for files containing records of known size so that you can move from one record to another using seek then read or change the records The records dont have to be the same size you just have to determine how big they are and where they are placed in the file At first its a little bit hard to believe that RandomAccessFile is not part of the InputStream or OutputStream hierarchy However it has no association with those hierarchies other than that it happens to implement the DataInput and DataOutput interfaces which are also implemented by DataInputStream and DataOutputStream It doesnt even use any of the functionality of the existing InputStream or OutputStream classes its a pletely separate class written from scratch with all of its own mostly native methods The reason for this may be that RandomAccessFile has essentially different behavior than the other IO types since you can move forward and backward within a file In any event it stands alone as a direct descendant of Object Essentially a RandomAccessFile works like a DataInputStream pasted together with a DataOutputStream along with the methods getFilePointer to find out where you are in the file seek to move to a new point in the file and length to determine the imum size of the file In addition the constructors require a second argument identical to fopen in C indicating whether you are just randomly reading r or reading and writing rw Theres no support for writeonly files which could suggest that RandomAccessFile might have worked well if it were inherited from DataInputStream The seeking methods are available only in RandomAccessFile which works 。