Tij Reading Notes (12th chapter)

Source: Internet
Author: User
Tags empty expression file size final functions connect split version
Note 12:java I/O system
Creating a good input/output (I/O) system is an extremely difficult task for programming language designers.
File class
Before we introduce a class that reads and writes data directly from a stream, let's introduce a class that processes files and directories.

You would think it was a class about the file, but it wasn't. You can use it to indicate the name of a file, or you can use it to indicate the name of a group of files in a directory. If it represents a set of files, you can also use the list () method to query, so that it returns a string array. Because the number of elements is fixed, the array is better than the container. If you want to get a list of another directory, then build a file object.
Directory list Device
Suppose you want to look at this directory. There are two ways. One is to call list () without parameters. It returns a complete list of what the file object contains. But if you want a "restricted list"-for example, if you want to see all the files with the extension. Java-then you have to use the "directory Filter" restricted. This is a class that is specifically responsible for selecting the contents of a file object display.

Declaration of the FilenameFilter interface:
Public interface FilenameFilter {Boolean accept (File dir, String name);


The Accept () method requires two parameters, one is a file object, which indicates which directory it is in, and the other is a string that represents the file name. Although you can ignore one or even two of them, you probably have to use the filename. Remember, the list () calls accept () for each file in the directory, and it is judged by whether it is included in the return value, which is based on the return value of accept ().

Remember that there is no path information in the filename. To do this you simply use a string object to create the file object, and then call the GetName () of the file object. It will help you peel away the path information (in a platform-independent way). Then, in accept (), the Matcher object of the regular expression (regular expression) is used to determine whether the regex matches the filename. After this circle, the list () method returns an array.
Anonymous inner class
Note that the parameter of the filter () must be final. You can only do this if you want to use an object outside of its scope in an anonymous inner class.

You can use anonymous inner classes to create one-off classes that are specific to a particular problem. The advantage of this approach is that it can concentrate all the code that solves a problem in one place. But from another perspective, this makes the code less readable, so be careful.
View and create a table of contents
The function of the file class is not limited to displaying files or directories. It can also help you create new directories or even directory paths (directory path) if the directories do not exist. It can also be used to check the properties of the file (size, last modified date, read-write permissions, etc.), to determine whether the file object represents files or directories, and to delete files.

Renameto () This method renames the file (or moves to) the new directory, which is the directory given by the parameter. And the parameter itself is a file object. This method also applies to directories.
Input and output
The I/O class library often uses the abstract "stream". The so-called "flow" is an object that can generate or accept data and represent the source and target of the data. The stream hides the specific operations inside the I/O device.

The Java I/O class library is divided into two parts, input and output. All InputStream and reader derived classes have a basic, inherited, read () method that reads a single or byte array. Similarly, all outputstream and writer derived classes have a basic write () method that writes a single or byte array. But usually you don't use these methods; they are for other classes-the latter will provide some more useful interfaces. So you rarely encounter situations where you can create a stream with just one class, and you actually have to stack multiple objects to get the functionality you need. The main reason for the Java streaming library to make a person dizzy is that "you have to use multiple objects to create a stream."
Types of InputStream
The task of InputStream is to represent classes that can get data from various input sources. These sources include:
The byte array string object file is similar to the pipelining pipeline (pipe). Put something from one end and let it out from the other. A "stream sequence (a sequence of other streams)" that can be assembled into a separate stream. Other sources, such as the Internet connection. (This section is discussed in the thinking in Enterprise java.) )
Each of these data sources has a subclass of the corresponding InputStream. In addition, FilterInputStream is a subclass of InputStream that provides the decorator (decorated) class for the base class, while decorator configures the properties and interfaces for the InputStream.

Table 12-1. InputStream types class function constructor parameter usage bytearrayinputstream buffer memory as InputStream the buffer from which to extract byte a data source: To connect it to the FilterInputStream object, The latter provides the interface. StringBufferInputStream A String object is required for InputStream. In fact, the inside of the program is StringBuffer. A data source: To connect it to a FilterInputStream object, which provides the interface. FileInputStream A String object that is used specifically to read a file, or it can be a file or FileDescriptor object. A data source: To connect it to a FilterInputStream object, which provides the interface. PipedInputStream extracts data from PipedOutputStream. Implement the piping feature. PipedOutputStream a data source in a multi-threaded environment, connecting it to the FilterInputStream object, provided by the latter interface. The Sequenceinputstream combines two or more inputstream into a inputstream. Two InputStream objects, or a Inputsteam object container enumerator a data source: To connect it to the FilterInputStream object, which provides the interface. FilterInputStream an abstract class that is used to define an interface for decorator. And the function of decorator is to realize specific function for InputStream. See table 12-3 for details. See table 12-3, see table 12-3
Types of OutputStream
This is part of the class that determines where to output: An array of byte (not string, but you can create a string from a byte array) or a file, or "pipe."

In addition, Filteroutputstream is the base class for the decorator class. It installs the properties and the applicable interface for the OutputStream.

Table 12-2. OutputStream class function Constructor parameter usage Bytearrayoutputstream creates a buffer in memory. The data is sent to the stream to write to this buffer. Buffer initial size, optional. To specify a target for the data, you can wrap it with Filteroutputstream and provide an interface. FileOutputStream writes data to a file. A string that represents a file name, or a file or FileDescriptor object. To specify a target for the data, you can wrap it with Filteroutputstream and provide an interface. The data that PipedOutputStream writes to this stream will eventually become the data source associated with the PipedInputStream. Otherwise, it will not be the "pipeline". PipedInputStream to specify a target for data in a multithreaded environment, you can wrap it with Filteroutputstream and provide an interface. Filteroutputstream an abstract class that provides an interface to decorator. And the function of decorator is to realize specific function for outputstream. As detailed in table 12-4, see table 12-4, see table 12-4
Adding attributes to the applicable interface
The practice of dynamically and transparently adding functionality to a single object using layered objects (layered objects) is called decorator pattern. (The pattern is the subject of thinking in Patterns, with Java.) Decorator mode requires that all objects that are wrapped outside the original object must have exactly the same interface. This makes the usage of decorator very transparent-regardless of whether the object is decorate or not, the message to it is always the same. This is why the Java I/O class library has the filter (filter) class: The Abstract "Filter" class is the base class for all decorator. (Decorator must have all the interfaces to the object it wraps, but decorator can extend the interface, thus deriving many "filter" classes).

Decorator patterns are often used in situations where the number of classes is unrealistic if you use inheritance to address a variety of requirements. Java I/O class libraries need to provide a combination of many functions, so the decorator model has a useful role. But decorator has the disadvantage of increasing the flexibility of programming (because you can easily mix and match attributes) and make the code more complex. The Java I/O class library is so weird because it "must create many classes for an I/O object", which is to add a lot of decorator to a "core" I/O class.

Classes that define the Decorator class interface for InputStream and OutputStream, respectively, are FilterInputStream and Filteroutputstream. These two names are not very good. Both FilterInputStream and Filteroutputstream inherit from the base class InputStream and OutputStream of the I/O class library, which is the key to the decorator model ( Only in this way can the interface of the decorator class be exactly the same as the object it wants to serve.
Read InputStream with FilterInputStream
FilterInputStream and its derived classes have two important tasks. DataInputStream can read various primitive and strings. (All methods begin with "read", such as ReadByte (), Readfloat ()). It, and its partner DataOutputStream, allows you to move primitive data from one place to another through a stream. These "Places" are listed in table 12-1.

Other classes are used to modify the internal behavior of the InputStream: whether to buffer, whether to know the line information it reads (allows you to read the line number or set the line number), whether it will eject a single character. The latter two look more like compilers (that is, they are probably designed for the Java compiler), so you usually don't use them very much.

No matter what type of I/O device you use, it is best to buffer it. So for the I/O class library, it is wiser to take the buffer as a special case (or to call the method directly), rather than the buffer as it is now.

Table 12-3. FilterInputStream class function Constructor parameter usage DataInputStream is used in conjunction with DataOutputStream so that you can be in a "portable way (portable fashion)" Reading primitives from the stream (Int,char,long, etc.) InputStream contains a complete set of interfaces for reading primitive data. Bufferedinputstream uses this class to solve the problem of "physically reading every time you want to use the data." You mean "use a buffer." "InputStream, and optionally the capacity of the buffer itself does not provide an interface, just provides a buffer." You need to connect to an "object with Interface (interface)." Linenumberinputstream the line number of the trace input stream; there are getlinenumber () and Setlinenumber (int) methods InputStream Just add a line number, so you have to have an "object with interfaces". Pushbackinputstream has a "suppression single byte" buffer (has a one byte push-back buffer) so that you can press back the byte that you read last. InputStream is primarily used for compiler scanners. may be designed for Java-enabled compilers. There are not many opportunities to use.
Use Filteroutputstream to write to OutputStream.
The other half of DataInputStream is DataOutputStream. Its task is to rearrange the primitive data and string objects into streams so that other machines can read the stream with DataInputStream. The DataOutputStream method starts with "write", such as WriteByte (), Writefloat (), and so on.

PrintStream's intention was to print primitive data and string objects in a way that everyone could read. This is different from the DataOutputStream, which is to load the data into a stream before handing it over to datainputstream for processing.

The two most important methods for PrintStream are print () and println (). Both of these methods have been overloaded, so you can print various data. The difference between print () and println () is that the latter prints a newline character more than one line.

Using PrintStream can be tricky because it captures all the ioexception (so you have to call CheckError () directly to check for error conditions, because this method returns True when it encounters a problem). Plus, PrintStream's internationalization is not doing well, and it's not a platform-neutral way to handle line-wrapping (these issues have been solved in printwriter, we'll talk about it later).

Bufferedoutputstream is a decorator, which represents a buffer of convection so that it does not physically operate every time it writes to the stream. The output is generally done.

Table 12-4. Filteroutputstream class function Constructor parameter usage DataOutputStream with DataInputStream, so you can use a "portable way (portable fashion)" Write primitive to the stream (int, char, long, etc.) OutputStream includes a full set of interfaces for writing primitive data. PrintStream is responsible for generating formatted outputs (formatted output). Dataoutputstrem is responsible for storing data, while PrintStream is responsible for the display of the data. A outputstream and an optional Boolean value. This boolean value indicates that you do not want to empty the buffer after the line break. Should be the final cladding layer of the OutputStream object. There are many opportunities to use. Bufferedoutputstream uses this class to solve the problem of "every time you write data to a stream, you have to physically manipulate it." That means "buffer." Empty the buffer with flush (). OutputStream, and an optional buffer size itself does not provide an interface, but adds a buffer. You need to link an object that has an interface.
Reader and writer Class department
Java 1.1 has made significant changes to the lowest I/O Stream class library. The first time you see reader and writer, you will feel that "they are probably used to replace InputStream and OutputStream" (like me). But that is not the case. Although some of the features of InputStream and OutputStream have been eliminated (the compiler warns if you continue to use them), they still provide a lot of valuable, byte-I/O capabilities. Reader and writer provide Unicode-compatible, character-oriented I/O capabilities. Furthermore
Java 1.1 also adds new additions to InputStream and outputstream, so it is clear that these two classes are not completely replaced. Sometimes, you must also use the "byte-based" and "character-based" classes. To do this, it also provides two "adapter (adapter)" classes. InputStreamReader is responsible for translating inputstream into reader, while OutputStreamWriter converts outputstream into writer.
Reader and writer to solve, the main problem is internationalization. The original I/O class Library supports only 8-bit byte streams, so it is not possible to handle 16-bit Unicode characters well. Unicode is an internationalized character set (not to mention Java's built-in char is a 16-bit Unicode character), so that all I/O will support Unicode after reader and writer are added. In addition, the new class library performance is better than the old.
Data sources and purposes
Almost all Java I/O streams correspond to the reader and writer that are designed to handle Unicode. But sometimes, byte-oriented InputStream and OutputStream are the right choices, especially Java.util.zip, whose classes are all byte-oriented. So the smartest thing to do is to use reader and writer, and when you have to use a byte-oriented class library, you will naturally know, because the program compiles.

The following table lists the relationships between the data sources and purposes of these two classes (that is, where the data comes from and where they go in these two classes of departments).
Data source and Destination Java 1.0 Class Java 1.1 class InputStreamReader adapter: Inputstreamreaderoutputstreamwriter Adapter: Outputstreamwriterfileinputstreamfilereaderfileoutputstreamfilewriterstringbufferinputstreamstringreader ( no corresponding Class) Stringwriterbytearrayinputstreamchararrayreaderbytearrayoutputstreamchararraywriterpipedinputstreampipedreaderpipedoutput Streampipedwriter
In short, these two classes, if not the same, are at least very similar.
Modify the behavior of a stream
Whether it is InputStream or outputstream, the time should be given to FilterInputStream and Filteroutputstrem, and by the latter, that is, decorator do some transformation. Reader and writer inherited this tradition, but not completely copied it.

The following table corresponds to a more sketchy relationship than the one in front. This is because the organizational structure of these two classes is different. For example, Bufferedoutputstream is a subclass of Filteroutputstream, but BufferedWriter is not a filterwriter subclass (the latter is an abstract class but has no subclasses. So it just seems to be a "seat", so you don't have to think about where it is. But anyway, their interfaces are very similar.
Class filterinputstreamfilterreaderfilteroutputstreamfilterwriter of Class Java 1.1 of the Filter Class Java 1.0 (this is an abstract class without derived classes) Bufferedinputstreambufferedreader (also ReadLine ()) Bufferedoutputstreambufferedwriterdatainputstream try to use DataInputStream (unless you use BufferedReader when you use ReadLine ()) Printstreamprintwriterlinenumberinputstream (OBSOLETE) Linenumberreaderstreamtokenizerstreamtokenizer (for another constructor, Pass reader as parameter to it) Pushbackinputstreampushbackreader
One is clear: stop using the DataInputStream ReadLine () (Compile-time warns you that this method is "obsolete" (deprecated)) and use BufferedReader. In addition, DataInputStream is still the "seeded" of the I/O class library.

In order to make the transition to printwriter simpler, PrintWriter has a constructor that takes OutputStream as a parameter, in addition to a constructor that takes the writer as a parameter. But the PrintWriter format is no better than the PrintStream; their interfaces are actually exactly the same.

The PrintWriter constructor also has an optional option to automatically empty the operation. If you set this tag, it will automatically empty after each println ().
A class that has not changed
When Java rose from 1.0 to 1.1, several classes did not change:
Class Dataoutputstreamfilerandomaccessfilesequenceinputstream for Java 1.0, which has no corresponding class in Java 1.1
Especially DataOutputStream, the usage is not changed at all, so you can use InputStream and outputstream to read and write the data that can transmit.
Self-formed: randomaccessfile
Randomaccessfile is used to access files that hold data records, so you can use the Seek () method to access records and read and write. These records do not have to be the same size, but their size and location must be knowable.

First of all, you may be less convinced that Randomaccessfile would not belong to the InputStream and OutputStream classes. In fact, in addition to implementing the Datainput and DataOutput interfaces (DataInputStream and DataOutputStream also implement these two interfaces), it has nothing to do with the two-class systems, There is not even a feature that is ready with InputStream and outputstream; it is a completely self-contained class, and all methods (most of which belong to itself) are written from scratch. This may be because randomaccessfile can move around in a file, so its behavior is fundamentally different from other I/O classes. In short, it is a standalone class that inherits object directly.

Basically, the way randomaccessfile works is to glue DataInputStream and dataoutputstream together with some of its own methods, such as the getfilepointer of positioning (), The Seek () to move in the file, and the length () that determines the file size. In addition, its constructor also has an argument for opening the file in read-only ("R") or read-write ("RW") (identical to the fopen () of C). It does not support write-only files, and from this point of view, if Randomaccessfile inherits DataInputStream, it may do better.

Only Randomaccessfile has a seek method, and this method applies only to files. Bufferedinputstream has a mark () method that you can use to set the tag (save the result in an internal variable) and then call Reset () to return to that position, but its functionality is too weak and not practical.

Most of the functionality of Randomaccessfile, if not all, has been replaced by the "Memory-mapped files (memory-mapped files)" of NiO in JDK 1.4. We'll talk about this part of the story below.
How common I/O flows are used
Although I/O streams can be combined in many ways, the most commonly used are the few. Under
Input stream
The first to part four demonstrates how to create and use InputStream. Part IV also briefly demonstrates the use of outputstream.
1. Buffer the input file
To open an open file to read characters, you first create a fileinputreader with a string or a file object. In order to improve speed, you should buffer the file, so you have to give the Fileinputreader reference to BufferedReader. Since BufferedReader also provides the ReadLine () method, it becomes the object you end up using, and its interface becomes the interface you use. When you read the end of the file, ReadLine () returns a null and then exits the while loop.

Finally, close () closes the file. Technically speaking, a finalize () should be called when a program exits (whether or not it is garbage), and finalize () calls Close (). However, the implementations of the various JVMs are inconsistent, so it is best to explicitly call Close ().

System.in is a inputstream, and BufferedReader needs a reader as a parameter, so you have to change hands by InputStreamReader first.
2. Read memory
The Read (StringReader) method treats the byte as an int, so if you want to print normally, you have to convert them to char first.
3. Read the formatted memory
To read "Formatted" data, you have to use DataInputStream, which is a byte-oriented I/O class (not oriented to char), so you can only Conto the InputStream. Of course you can think of everything (like a file) as byte and read it with InputStream, but here is string. To turn a string into a byte array, you can use the GetBytes () method of string, and Bytearrayinputstream can handle the byte array. At this point, you don't have to worry about not having the right inputstream to create the DataInputStream.

If you are using ReadByte () bytes to read DataInputStream, then regardless of the byte value is legal, so you can not according to the return value to determine whether the input is over. You can only use available () to determine how many characters there are.

Note that available () works differently depending on the read media; Strictly speaking, it means "the number of bytes that can be read without blocking." "For a file, it's the entire file, but if it's another stream, it's not necessarily the case, so stay simpleton before you use it."

You can also like this, with the exception to check the input is not finished. But in any case, using the exception as a control process is always an abuse of this function.
4. Read the file
(Try removing the bufferedwriter and you can see its impact on performance-buffering can significantly improve I/O performance).

Linenumberinputstream It's a stupid, useless class.

After the input stream is exhausted, readLine () returns NULL. If you write a file without calling close (), it will not empty the buffer, so it is possible to drop something.
Output stream
Depending on how the data is written, OutputStream is divided into two main categories: one for people to see, the other for DataInputStream. Although the Randomaccessfile data format is the same as DataInputStream and DataOutputStream, it does not belong to OutputStream.
5. Storing and recovering data
PrintWriter will format the data so that people can read it. But if the data output, but also to restore the other flow, then you have to use DataOutputStream to write the data, and then use DataInputStream to read the data. Of course, they can be any stream, but we're using a buffered file here. DataOutputStream and DataInputStream are byte-oriented, so these streams must all be inputstream and outputstream.

If the data is written in DataOutputStream, the datainputstream can accurately restore it on either platform. This is really useful because no one knows who is worrying about the data that is dedicated to the platform. If you're using Java on two platforms, the problem doesn't even exist.

When writing strings with DataOutputStream, the only way to make sure that you can recover them in the future is to use the UTF-8 code, which is like the 5th part of the routine, with writeUTF () and readUTF (). UTF-8 is a variant of Unicode. Unicode represents one character in two bytes. However, if you're dealing with all of them, or mostly ASCII characters (only 7 bits), then it would be wasteful to look at both the storage space and the bandwidth, so UTF-8 uses a byte to represent the ASCII character, and two or three bytes for non-ASCII characters. In addition, the length information of the string exists in the first two bytes of (string). writeUTF () and readUTF () are using Java's own UTF-8 version, so if you want to read the string written by writeUTF () with a Java program, you have to do some special processing.

With writeUTF () and readUTF (), you can safely mix string and other data to DataOutputStream, because you know that string is stored in Unicode, And it can be easily recovered with DataOutputStream.

Writedouble () writes a double to the stream, and its shadow Readdouble () is responsible for restoring it (other data has a similar read-write method). But in order for the reading method to work, you have to know what data is placed in each position of the stream. Because you can read a double as a byte,char, or something. So either write the file in a fixed format or provide additional explanatory information in the file, and then look for the data while you're reading the data. First of all, the serialization of objects can be simpler for storing and restoring complex data.
6. Read and write random files
As we said earlier, if it does not implement the Datainput and DataOutput interfaces, Randomaccessfile is almost entirely independent of other I/O class libraries, so it cannot be combined with InputStream and OutputStream. While using Bytearrayinputstream as a "random-access element" is a reasonable thing, you can only use Randomaccessfile to open the file. Moreover, you can only assume that Randomaccessfile has already buffered, because there is nothing you can do if you do not.

The second parameter of the constructor means that the Randomaccessfile is opened as read-only ("R") or read-write ("RW").

The use of Randomaccessfile is like the combination of DataInputStream and DataOutputStream (because their interfaces are equivalent). In addition, you can use Seek () to move up and down in the file and modify it.

With the advent of new I/O to JDK 1.4, you should consider whether to replace Randomaccessfile with "memory-mapped Files" (memory-mapped file).
Pipe flow
This chapter will only give a general reference to Pipedinputstream,pipedoutputstream,pipedreader and PipedWriter. This is not to say that they are not important, just because the pipe flow is used for communication between threads, so unless you have understood multithreading, you will not understand its value. We will use an example in the 13th chapter to explain this problem.
Utilities for reading and writing files
Read the file into memory, change it, and then write the file. This is a more common programming task. But there is this problem with Java I/O, and even with this routine, you have to write a bunch of code-there's no auxiliary function at all. What's worse, the distracting decorator will make you forget how to open the file. So it's wiser to write a secondary class yourself. Here's a class that contains a static method that lets you read and write text files as strings. In addition, you can create a textfile class that "stores the contents of a file in ArrayList" (so that you can use the ArrayList function when working on a file):

: com:bruceeckel:util:textfile.java//Static functions for reading and writing text files as//a single string, and Trea Ting a file as an arraylist.//{Clean:test.txt test2.txt}package com.bruceeckel.util;import java.io.*;import java.util.* ;p ublic class Textfile extends ArrayList {//Tools to read and write files as single strings:public static String read (S Tring FileName) throws IOException {StringBuffer sb = new StringBuffer (); BufferedReader in = new BufferedReader (new FileReader (FileName)); String s; while (s = in.readline ())!= null) {sb.append (s); Sb.append ("\ n");} in.close (); return sb.tostring (); public static void Write (string fileName, String text) throws IOException {PrintWriter out = new PrintWriter (New Buffe Redwriter (New FileWriter (FileName)); Out.print (text); Out.close (); Public Textfile (String fileName) throws IOException {super (Arrays.aslist (FileName). Split ("\ n"))} public void W Rite (String fileName) throws IOException {PrintWriter out = new PrinTwriter (New BufferedWriter) (New FileWriter (FileName)); for (int i = 0; i < size (); i++) out.println (Get (i)); Out.close (); }//Simple test:public static void main (string[] args) throws Exception {String file = Read ("Textfile.java"); Write ("Te St.txt ", file); Textfile text = new Textfile ("test.txt"); Text.write ("Test2.txt"); }} ///:~

All of these methods will be thrown directly out of the IOException. Since a line is read, the following line breaks are gone, so read () adds a newline character to the back of each line, and then to the back of the stringbuffer (for efficiency reasons). Finally, it returns a string that "contains the contents of the entire file." The task of write () is to open the file and write something inside it. Remember to give the file to close () after the task is complete.

The constructor of (Textfile) converts a file to a string using the Read () method, and then transforms the array with String.Split () and newline characters (if you want to use this class frequently, you should probably rewrite the constructor to improve performance). In addition, because there is no corresponding join method, the non-static write () method can only manually print the file line-by-row.

To make sure it works, main () makes a basic test. Although it's a small program, you'll find that it helps you save a lot of time and make life easier.
Standard I/O
"Standard I/O" is the concept of Unix, meaning that a program uses only one stream of information (this design idea is also embodied in some form on windows and many other operating systems). All input comes in from "standard input", the output goes out from "standard output", and the error message is sent to "standard error". The advantage of standard I/O is that it makes it easy to concatenate programs and make the output of one program the input of another program. This is a very powerful feature.
Read standard input
Java follows the standard I/O model, providing syetem.in,system.out, as well as System.err. This book has always been written with System.out to standard output, and it (System.out) is an object that has been previously processed and packaged as a printstream. Like System.out, System.err is also a printstream, but system.in is wrong, it is an untreated inputstream. In other words, although you can directly to System.out and System.err write, but want to read system.in, you have to do first processing.

Usually, you will read the input with ReadLine () line by row, so wrap the system.in into BufferedReader. But before that you have to use Inputsteamreader to convert system.in into reader.
Convert System.out to PrintWriter
System.out is PrintStream, which means it is outputstream. But PrintWriter has a constructor that can transform OutputStream into PrintWriter. With this constructor, you can convert System.out to printwriter at any time:

In order to start automatically emptying the buffer, be sure to use the two-parameter version of the constructor and set the second argument to true. This is important, otherwise you may not be able to see the output.
Redirection of standard I/O
The Java System class also provides several static methods that allow you to redirect standard input, standard output, and standard errors:

Setin (InputStream) setout (printstream) seterr (PrintStream)

If the program in a short period of time output a lot of information, so that the screen is very fast, so that you can not read, then the output to redirect will appear very useful. It is also important to redirect input for command-line programs that repeat test user input.

I/O redirects are not character streams, but byte streams, so you can't use reader and writer to use InputStream and OutputStream.


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.