Java-Java file (read/write) Input/Output

Source: Internet
Author: User

1. Stream:
It transmits data from the producer (such as the keyboard, disk file, memory, or other devices) to the consumer (such as the screen, file, or memory) receiving the data through a buffer mechanism) the abstraction of this process.
2. Related Java packages:
The Java. io package contains many classes that provide various operations on objects.
3. classes related to file names and directory names:The File class is independent of the system platform and uses Constructor
File (String path ),
File (String path, String FileName ),
File (File dir, String name) and so on to create a File object; then use canRead (), canWrite (), getParent (), getPath () and other member functions.
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. exists ())
{System. err. println ("Cant 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. File Content (data) operations:
4.1 input/output abstraction base class InputStream/OutputStreamThe basic functions of File Content operations include read (), write (), close (), and skip (). Generally, the object of the derived class is created (to complete the specified special functions) to read and write files. During file read/write programming, you should pay attention to the exception handling technology.
4.2 FileInputStream/FileOutputStream:
Used to read and write local files (read and write files in binary format and in sequence, and create different file stream objects for read and write operations respectively );
The basic process of local file read/write programming is:
① Generate a file stream object (FileInputStream class should be used for file read operations, and FileOutputStream class should be used for file write );
② Call functions in the FileInputStream or FileOutputStream class, such as read () and write (int B), to read and write the file content;
③ Close ()).
4.3 PipedInputStream/PipedOutputStream:
Used for pipeline Input and Output (the output result of a program or thread is directly connected to the input port of another program or thread for direct transmission of data between the two. Link required during operation );
4.3.1 pipe connection:
MethodIs to use the constructor to directly use the output of a program as the input of another program, specifying the target pipeline object when defining the object
PipedInputStream pInput = new PipedInputStream ();
PipedOutputStream pOutput = new PipedOutputStream (pInput );
Method 2Is connected by using any member function connect () in both classes.
PipedInputStream pInput = new PipedInputStream ();
PipedOutputStream pOutput = new PipedOutputStream ();
Pinput. connect (pOutput );
4.3.2 MPs queue input and output:
The output pipeline object calls the write () member function to output data (that is, send data to the input of the pipeline), and the input pipeline object calls read () member functions can read data (that is, obtain data from the output pipeline ). This is mainly achieved through the system's buffer mechanism.
4.4 random file reading/writing:
The RandomAccessFile class (which directly inherits from the Object class rather than the InputStream/OutputStream class) can read and write data in any position of the file (only the pointer to the file's read/write position needs to be changed ).
The basic process of random file read/write programming is:
① Generate a stream object and specify the read/write type;
② Mobile read/write location;
③ Read and Write File Content;
④ Close the file.
StringBuffer buf = new StringBuffer ();
Char ch;
While (ch = (char) System. in. read ())! =)
{
Buf. append (ch );
} // The read/write mode can be "r" or "rw"
RandomAccessFile myFileStream = new RandomAccessFile ("myFile. dat", "rw ");
MyFileStream. seek (myFileStream. length ());
MyFileStream. writeBytes (buf. toString (); // Add the content entered by the user from the keyboard to the end of the file
MyFileStream. close ();
4.5 DataInput/DataOutput interface:Read/write (such as readChar (), readInt (), readLong (), and readFloat (), and readLine () returns a String ). The RandomAccessFile class implements this interface and has a more flexible data read/write method than the FileInputStream or FileOutputStream class.
4.6 standard input and output streams:System. in (for example, char c = System. in. read () and System. out (for example, System. out. println (), System. out. println ()).
Try
{Char ch = System. in. read (); // returns binary data (the low 8-bit ASCII code of the keyboard)
}
Catch (IOException e)
{
}
4.7 general file operations:
(1) generate an object of the input/output file class (based on the type to be operated );
(2) call such member functions to read and write file data;
(3) close the file.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.