Java input Stream

Source: Internet
Author: User

Of course, one of the things we often want to do is print the formatted output to the console, but that is simplified in the Com.bruceeckel.tools created in chapter 5th.
Sections 1th through 4th demonstrate the creation and use of the input stream (although Part 4 shows the simple application of the output stream as a test tool).

1. Buffered input File
To open a file for input, you need to use a fileinputstream and use a string or file object as the filename. To improve speed, it is best to buffer the file to obtain the result handle for a bufferedinputstream builder. To read the input data in a formatted form, we assign that result handle to a datainputstream builder. DataInputStream is our final (final) object and is the interface for our read operations.
In this example, only the ReadLine () method is used, but of course any DataInputStream method can be used. Once you reach the end of the file, ReadLine () returns a null (NULL) to abort and exit the while loop.
"String S2" is used to aggregate the complete file contents (including new rows that must be added because ReadLine () removes those rows). Subsequently, use S2 in the later part of this program. Finally, we call Close (), which closes the file. Technically, close () will be called when Finalize () is running. And we want this to happen once the program exits, whether it's garbage collection or not. However, Java 1.0 has a very prominent error (BUG), which does not occur. In Java 1.1, you must explicitly call System.runfinalizersonexit (true) to ensure that Finalize () is invoked for each object in the system. However, the safest approach is to explicitly call Close () for a file.

2. Input from memory
This section takes a string s2 that already contains the full file content and uses it to create a stringbufferinputstream (string buffered input stream)--as a builder parameter, requiring a string rather than a stringbuffer. We then read each character in reading () and send it to the console. Note that read () returns the next byte to int, so it must be styled as a char to print correctly.

3. Format Memory input
The StringBufferInputStream interface is limited, so it is often necessary to encapsulate it in a datainputstream, thereby enhancing its capabilities. However, if you choose to read one character at a time with ReadByte (), all values are valid, so you can no longer use the return value to detect when the input ends. Instead, you can use the available () method to determine how many characters are available. The following example shows how to read one character at a time from a file:

//: Testeof.java//testing for the "end of file" while reading//a byte at a time. Import java.io.*; public class Testeof {public static void main (string[] args) {try {datainputstream in = new Datai
      Nputstream (New Bufferedinputstream) (New FileInputStream ("Testeof.java"));
    while (in.available ()!= 0) System.out.print ((char) in.readbyte ());
    catch (IOException e) {System.err.println ("IOException"); }
  }
} ///:~

Note that depending on what media you are currently reading from, the way avaiable () works is different. It literally means "the number of bytes that can be read without blocking." For a file, it means the entire file. But for a different kind of data flow, it may have different meanings. Therefore, should be considered in the use of comprehensive.
In order to detect the end of the input in such a case, it can also be achieved by capturing an illegal violation. However, it is a bit overqualified to control the flow of data by unauthorized means.

4. Line numbering and file output
This example shows how to Linenumberinputstream to track the number of input lines. Here, you can't simply combine all the builders because you have to keep a handle on the Linenumberinputstream (note that this is not an inheritance environment, so you can't simply in4 the shape to a linenumberinputstream). Therefore, Li holds a handle to the linenumberinputstream and then creates a datainputstream on it to read the data.
This example also shows how to write formatted data to a file. First, you create a fileoutputstream that connects to the same file. Given the efficiency factor, it generates a bufferedoutputstream. This is almost certainly our usual practice, but it must be done explicitly. It is then converted to a printstream for formatting. The data file created in this manner can be read as an original text file.
One way to flag datainputstream when it ends is ReadLine (). Once no more strings are available to read, it returns NULL. Each line is printed to the file with its own line number. The line number can be queried by Li.
You can see an explicitly specified close () for OUT1. This is quite useful if the program is ready to turn around and read the same file again. However, the program did not check file IODemo.txt until the end. As previously noted, if you do not call Close () for all of your output files, you may find that the buffer will not be refreshed, causing them to be incomplete.

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.