Java's Document Read and Write December 09, 2004 Java's Document Read and Write the BLOG Java of YJQ8116 (read and write) input and output (transfer) 1, stream: it is through the buffer mechanism from the producer ( If the keyboard, disk file, memory, or other device is transmitted to the process of consumers (such as screens, files or memory, etc.) to accept the data. 2. The relevant Java package: java.io package includes many classes to provide many aspects of the relevant documents. 3. Categories of file name and directory name: File class is independent of the system platform, using the constructor file (String Path), File (String Path, String Name), File (File Dir, String Name), etc. Create a File object; Use canread (), canwrite (), getParent (), getPath () and other member functions to implement the operation of each attribute to the file. Import java.io. *; public class filetest {public static void main (string "args) {string filename =" c: //temp//myfile.dat "file myfile = new file (filename); if (! myfile ()) {System.err.Println ("can't find" filename); return;} system.out.println ("file" filename "is" myfile.length () "bytes long ! "); If (myfile. Isdirectory ()) {system.err.println (" file " filename " is a directory! "); Return;}}} 4, related to file content (data) operation class: 4.1 Input Output Abstract Foundation INPUTSTREAM / OUTPUTSTREAM, implementing the basic function of file content operation Read (), Write (), close (), Skip (), etc .; generally create its derived class object (complete specified special features) ) To implement file reading and writing. Technologies should be paid to abnormal processing in the programming process of document reading and writing. 4.2 FileInputStream / FileOutputStream: Used for local file reading and writing (binary format reading and writing and is sequential reading and writing, reading and writing to create different file stream objects); the basic process of reading and writing programming of local files is: 1 Generate file stream Object (I should be a FileInputStream class when reading the file, and the file write should be the fileoutputstream class); 2 Call the function function in the fileInputstream or FileOutputStream class, such as read (), write (int b), etc.) read and write file content; 3 Close File (Close ()). 4.3 PipedInputStream / PipedputStream: Used for pipe input and output (directly connected to another program or a thread input port directly to another program or a thread.
Need to connect when operating); 4.3.1 Pipeline connection: One of the methods is to direct the output of a program as another program input by constructive function, indicating the target pipe object pipedinputStream PINPUT = New PipedInputStream (); PipedOutputStream pOutput = new PipedOutputStream (pInput); the second method is to use any of the two sides of a class member function connect () connected PipedInputStream pInput = new PipedInputStream (); PipedOutputStream pOutput = new PipedOutputStream (); pinput.connect (pOutput); 4.3.2 Input and Output of Pipes: Output Pipe Object Call WRITE () member function output data (i.e., send data to the input of the pipe); and the input pipe object calls the read () member function to read data (ie from output pipeline Get data. This is mainly implemented by means of a buffer mechanism provided by the system. 4.4, random file read / write: RandomaccessFile class (it directly inherits the Object class and non-inputStream class), so that data in any location in the read / write file (pointer only needs to change the read and write position of the file). The basic procedure for the read and write programming of the random file is: 1 generate flow objects and indicate the type of read and write; 2 Move read and write position; 3 read and write file content; 4 Close the file. Stringbuffer buf = new stringbuffer (); char ch; while ((CH = (char) system.in.read ())! = '/ N') {buf.append (ch);} // read and write mode can be "r" or "rw" randomaccessfile myfilestream = new randomaccessfile ("myfile.dat", "rw"); myfilestream.length ()); myfilestream.writebytes (buf.tostring ()); // Put the user Add from the keyboard input to the end myFileStream.close (); 4.5 DataInput / DataOutput interface: implementation with the machine-independent of the various data formats read and write (such as readchar (), readint (), readlong (), readfloat () And readline will return a string). The RandomaccessFile class implements the interface, which has a more flexible data read and write than the FileInputStream or FileoutPutStream class. 4.6 Standard input and output flow: system.in (such as: char c = system.in.read ()) and system.out (such as: system.out.println (), system.out.println ()).