Java's file read and write

xiaoxiao2021-03-06  36

1, stream:

It is an abstraction that transmitting data from producers (such as keyboards, disk files, memory, or other devices) through the buffer mechanism to accept the process of consumers (such as screen, file or memory, etc.).

2, related Java package:

The Java.IO package includes many classes to provide many aspects of the relevant documents.

3. Categories related to file name and directory name: File class is independent of the system platform, using constructor

File (String Path),

File (String Path, String FileName),

File (File Dir, String Name) Creates a File object; then uses canread (), canwrite (), getParent (), getPath () and other member functions to implement the individual properties of the file.

Import java.io. *;

Public Class FileTestSt

{public static void main (String [] ARGS)

{

String filename = "c: //temp/MYFILE.DAT"

File myfile = new file (filename);

IF (! Myfile. Exissrs ())

{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. Categories related to file content (data) operations:

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 in local file read and write (binary format read and write and is sequential reading and writing, reading and writing to create different file stream objects);

The basic procedure for reading and writing of local files is:

1 Generate a file stream object (which should be a fileInputstream class when reading the file, and the file should be a FileoutputStream class);

2 Call the FileInputStream or FileOutputStream class function function such as read (), Write (int b), etc.) read and write file content;

3 Close the file (Close ()).

4.3 PipedInputStream / PipedputStream:

Used for pipe input and output (directly connected to another program or a thread to another program or a thread input port, implementing the direct transfer of the two data. It is necessary to link when operating);

4.3.1 Pipeline connection:

One of the methods is to direct the output of a program as another program by constructor, indicate the target pipe object when defining the object.

PipedinputStream Pinput = new pipedinputstream (); pipedoutputstream poutput = new pipedoutputstream (pinput);

The second method is to connect to any member function Connect () in both parties.

PipedinputStream Pinput = New PipedInputStream ();

PipedputStream Poutput = New PipedoutputStream ();

Pinput.connect (poutput);

4.3.2 Input and Output of Pipes:

Output pipe object calls the Write () member function output data (i.e., sending data to the input of the pipe); and the input pipe object calls the READ () member function can read data (ie, obtain data from the output pipe). This is mainly implemented by means of a buffer mechanism provided by the system.

4.4, random file reading and writing:

The RandomaccessFile class (it directly inherits the Object class and non-inputStream class), so that data in any location in the read / write file (a pointer to the reading and writing position of the file) can be implemented.

The basic procedure for the read and write programming of the random file is:

1 generate flow objects and indicate the type of reading and writing;

2 mobile reading 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. Seek (MyFileStream.length ());

MyFileStream.WriteBytes (buf.tostring ()); // Add the user from the keyboard input to the end of the file

MyFileStream.Close ();

4.5 DataInput / DataOutput interface: Realize various data formats that have nothing to do with the machine (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 ()).

Try

{char ch = system.in.read (); // Return binary data (low 8 bit as a keyboard ASCII code)

}

Catch (IOException E)

{

}

4.7, the general method of file operation:

(1) Generate an object of an input output file class (depending on the type of operation);

(2) Call the member function of such a class to implement the read and write of the file data content;

(3) Close this file.

转载请注明原文地址:https://www.9cbs.com/read-66849.html

New Post(0)