In the entire Io package, the print stream is the most convenient class for output information, mainly including the byte print stream and the printwrite ). The print stream provides a very convenient printing function that can print any data type, such as decimal places, integers, strings, and so on.
Let's take a look at the construction method of printstream:
Public printstream (outputstream out)
In the constructor defined in printstream, we can clearly find that a constructor can directly receive instances of the outputstream class, because compared with outputstream, printstream can output data more conveniently, this is like re-packaging the outputstream class to make the output more convenient.
Output Information Using printstream:
1 Import Java. Io. file;
2 Import Java. Io. fileoutputstream;
3 Import Java. Io. ioexception;
4 Import Java. Io. printstream;
5
6 Public Class Test24 {
7 Public Static Void Main (string [] ARGs) Throws Ioexception {
8 File F = New File ("D:" + file. Separator + "test.txt ");
9 Printstream output = New Printstream ( New Fileoutputstream (f ));
10 Output. println ("Hello world! ");
11 Output. Print ("1 + 1 =" + 2 );
12 Output. Close ();
13 }
14 }
That is to say, the fileoutputstream class is actually packaged. This design is calledDecoration Design.
However, if it's just the above functions, it would be too cool. Well, let's take a look. You 've learned C ~ :
1 Import Java. Io. file;
2 Import Java. Io. fileoutputstream;
3 Import Java. Io. ioexception;
4 Import Java. Io. printstream;
5
6 Public Class Test25 {
7 Public Static Void Main (string [] ARGs) Throws Ioexception {
8 File F = New File ("D:" + file. Separator + "test.txt ");
9 Printstream output = New Printstream ( New Fileoutputstream (f ));
10 String name = "Jim ";
11 Int Age = 20;
12 Float Score = 90.5f;
13 Char Sex = 'M ';
14 Output. printf ("Name: % s age: % d score: % F gender: % C", name, age, score, sex );
15 Output. Close ();
16 }
17 }
Haha, printf exists !~ And if you think% S % d % F % C is too troublesome and can be replaced by % s, Better than C's printf