Java Basic notes (iii: Files and Data streams)

Source: Internet
Author: User

One input stream and output stream

The input stream loads data from files, standard input, or other external input devices into memory. The output stream acts just the opposite, saving the data in memory to a file, or transmitting it to the output device. The input stream corresponds to the abstract class Java.io.InputStream and its subclasses in the Java language, and the output stream corresponds to the abstract class Java.io.OutputStream and its subclasses. Abstract classes java.io.InputStream and java.io.OutputStream Define the basic operations of the input and output streams.

( 1) inputstream and fileinputstream

  java.io.inputstream is an abstract class, so you cannot pass the new InputStream () "the way constructs instance object. But it defines the basic operation of the input stream, such as reading data ( read ) and close the input stream ( close ) and so on. system.in Yes java.io.inputstream

class Java.io.FileInputStream are subclasses of abstract class Java.io.InputStream . Creates An instance object of class Java.io.FileInputStream that can be created by using the class java.io.FileInputStream construction method. The basic steps for manipulating the contents of a file are as follows:

1) Create An instance object of the input/ output stream or reader for the file to obtain the relevant system resources, for example, the memory space for storing the file information and the control permission for the file;

2) read (input)/ Write (output) operation of the file;

3) finally call the close member method to close the file to release the system resources that are occupied.

(2) outputstream and fileoutputstream

abstract class Java.io.OutputStreamis the class that is used to process the output stream. It defines the various basic operations of the output stream, such as: Output data (Write) and close the output stream. System.outis aJava.io.OutputStreamvariable of type, which corresponds to standard output, is used primarily to output information in the console window. In general, abstract classes are calledJava.io.OutputStreamof theWritethe abstract class is often called after the member methodJava.io.OutputStreamthe Force output member methodFlush (). This is often necessary because the current computer system is used to improve operational efficiency, often using a caching mechanism. This way, when the member method is calledWriteafter that, the data is often not output or written directly to the file, but is temporarily saved in the cache. When the data accumulates to a certain extent, the data is actually output outward. Calling member MethodsFlushis to force the output of the data immediately. This way, when the member method is calledFlushafter that, the output is generally visible immediately.

class Java.io.FileOutputStream are subclasses of abstract class Java.io.OutputStream . the instance object that creates the class Java.io.FileOutputStream is typically done through its construction method, which can call the corresponding write member method to write the data to the file. And closes the output stream through the close member method. The steps for manipulating the file are as follows:

1) Create an instance object of the class Java.io.FileOutputStream to obtain the relevant file resources;

2) writes the data to the file through the write member method of the class Java.io.FileOutputStream , in the middle, and through the class the flush member Method of the Java.io.FileOutputStream forces the output;

3) Finally, call the close member method of the class Java.io.FileOutputStream to close the file to free up the system resources that are occupied.

Create An instance object of class Java.io.FileOutputStream and call write for class Java.io.FileOutputStream member methods, and so on, can produce exceptions. Therefore, it is generally necessary to use try-catch structure or try-catch-finally structure to deal with the corresponding anomalies.

(3) PrintStream

class Java.io.PrintStream is a very important output stream class. The System.out that represents the standard output and is used to output information in the console window is a variable of type Java.io.PrintStream. The class Java.io.PrintStream has very good characteristics:

1) It contains different member methods that can be used to directly output multiple types of data;

2) Most of its member methods do not throw exceptions;

3) You can choose whether to use the Auto force output (flush) feature. If the auto-force output feature is used, the data in the cache is generally automatically written to the specified file or displayed in the console window when the output returns to a newline.

(4) input and output streams for data

The classes corresponding to the output stream and the input stream of the data are class Java.io.DataInputStream and java.io.DataOutputStream, respectively. is primarily used to read and store data of the base data type, and each base data type data stores the same number of bytes as it occupies in memory, such as an integer (int) type of data that occupies 4 bytes. Because the storage format of the data flow is in a uniform form, the input stream of the data is less relevant to the platform of the output stream. In general, the input and output streams should be used with each other, for example: Data input stream (datainputstream) is used toread the data output stream (dataoutputstream ) stored data.

(5) input and output streams with cache

The classes that correspond to the input stream with the cache and the output stream are java.io.BufferedInputStream and java.io.BufferedOutputStream. These two classes further enhanceThe efficiency of the input streamand the output streamto read and store data through a caching mechanism. When you create An instance object of Java.io.BufferedInputStream or Java.io.BufferedOutputStream, will open up in memory a byte array of storage units (commonly called caches) that are used to hold data in the end of the data stream. Thus, with the aid of byte array caching, a large chunk of data can be read or stored in memory, or a large chunk of memory will be written to the specified file at once, thus improving read / write efficiency.

(6) Redirection of standard input and output streams

class Java.lang.Systemcontain3a static member fieldinch, outand theErr, representing standard input streams, standard output streams, and standard error output streams, respectively. The standard input stream is primarily used to accept keyboard input. The standard output stream is used to output information in the console window. The standard error output stream is used to output error messages in the console window. Because they're all classes .Java.lang.Systemstatic member domain, so it can be accessed directly through the class name, i.e.system.in,System.outand theSystem.err. They are of the typeJava.io.InputStream,Java.io.PrintStreamand theJava.io.PrintStream, you can do a variety of things by calling the member methods of these classes.

The redirection of the standard input stream, standard output stream, and standard error output stream is the corresponding relationship between these standard input streams, the standard output stream, and the standard error output stream, respectively, with the specified file, and when data is required, the data is read from the file, and the data is written to the file when data is required to be output.

Two Reader / writer

the input and output streams mentioned above are all accessed in bytes as basic units, and can be thought of as processing byte streams. And the reader is the basic unit of characters to access the file, so that the reader can be considered to handle a character stream.

(1) Reader and Writer

abstract class Java.io.Reader and abstract class java.io.Writer prescribe some basic operations of the reader. Abstract class Java.io.Reader is used to read data, and abstract classes java.io.Writer used to store data.

You cannot generate abstract classes directly with "new Reader ()" or "new Writer ()" Java.io.Reader or abstract class Java.io.Writer instance objects, which can only be generated by the subclasses of the abstract class for their child classes.

(2) FileReader and FileWriter

class Java.io.FileReader and class java.io.FileWriter are abstract classes, respectively Java.io.Reader and Abstract Classes Java.io.Writer sub-class. Class Java.io.FileReader is compatible with all member methods of the abstract class Java.io.Reader , which can read characters and turn off character streams. Class Java.io.FileWriter is compatible with all member methods of the abstract class java.io.Writer and can be used to output single or multiple characters, Forces the output and closing of a character stream.

(3) reader with cache

     Java.io.bufferedreader and java.io.bufferedreader and class java.io.linenumberreader Inherit abstract class java.io.reader All member methods, You can perform operations such as reading characters and closing character streams. Class java.io.bufferedwriter Inherit abstract class java.io.writer All member methods, You can output single or multiple characters, force output, and turn off character streams.

(4) PrintWriter

class Java.io.PrintWriter is a very important character stream class. It is very similar to the class java.io.PrintStream . The class Java.io.PrintStream is stronger when processing bytes, while class Java.io.PrintWriter is advantageous in character processing. similar to class Java.io.PrintStream , class java.io.PrintWriter has very good characteristics:

    1) Java.io.printwriter printwriter The Member method of public boolean checkerror () true false

    2) Java.io.printwriter implements the class print member method, so printwriter Span style= "font-family: the song Body;" > also has different member methods that can directly output multiple types of data.

3) Auto Force output (flush) function of class Java.io.PrintWriter and Class Java.io.PrintStream The corresponding functions are different. When printwriter is used , only its member method println,printf , or Format is not likely to force output automatically.

(5) reading data from a console window

Classes java.io.InputStreamReader and java.io.OutputStreamWriter are common function classes that read data from the console, Through these two classes, the input stream and output stream can be converted to the corresponding reader, thus facilitating the processing of text data.

Three file

When a file that needs to read the data does not exist, it is often not possible to make a direct judgement, but must pass the exception to get this information, and class java.io.File a good solution to the problem. Class java.io.File generally does not involve the content inside the file, but the whole process of the file, such as obtaining a variety of file information or delete files. Class java.io.File can not only manipulate files, but also manipulate paths.

Java Basic notes (iii: Files and Data streams)

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.