Java Io study notes (7): print a stream

Source: Internet
Author: User

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

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.