Summary of Io operations in Java (III)

Source: Internet
Author: User
ArticleDirectory
    • Category
    • Printwriter class
    • Io support for the system class

To be honest, I don't really like the Java language. Although it is very powerful, there are many ready-made APIs that can be called.

But I always feel that it makes simple things too complicated and sometimes gets lost.

I cannot tell whether it is for writing or for the language itself.

My first courseProgramming LanguageIt's Python, although I'm not very familiar with it

However, its simplicity and elegance are unforgettable (ER, actually two years ago ......)

The second language I came into contact with is C, which gives me the feeling of being pure, efficient, and flexible.

Instead of Java, write a bunch of vagueCodeTo implement a small function

Frankly speaking, if a person is learning something he is not interested in, it will be very tired.

Supporting me, I have an unusual yearning for mobile development, and I like android very much. The main language for Android development is Java.

Although I have been studying for half a year, I still cannot explain what kind of enterprise development does Shenma have.

What I want is simply to treat programming as a flirt in my life. If I want a function, I will implement it in the simplest way possible.

Even my guess is that, when we develop in the future, the technology of programming is like everyone uses office.

SoProgramHow hard should you be?

You know, it's terrible for a person to put his or her own interests above his or her real thoughts ......

Therefore, everyone should use what they like and how efficient they are to do what they want most.

Well, I have only one purpose.

Will I tell you that I don't understand at all, and I don't want to understand the complicated garbage Syntax of Java?

I only use the simplest and most useful things ......

I am too lazy to record the previous Io writing methods in Java, mainly because the system class supports Io.

If you think that the code I wrote is not deep enough, you can spray it. What else can you do after the injection?

 

Now, step into the topic ......

In this section, we will discuss the usage of the category class and the printwriter class.

 

CATEGORY instance 1: read from the keyboard
 
Import Java. util. extends; public class demo {public static void main (string [] ARGs) {extends input = new loads (system. in); system. out. println ("Please output an integer:"); int I = input. nextint (); system. out. println ("the integer you entered is:" + I );}}

The preceding example only shows how to read an integer. Of course, there are methods to read floating point numbers and other data types. This is relatively simple. You can check the API.

 

Instance 2: read from string
Import Java. util. break; public class demo {public static void main (string [] ARGs) {// here \ r \ n is a line break, in Linux, only \ n can be used to export input = new bytes ("Hello \ r \ nworld \ r \ n"); // read cyclically, hasnext () in the same way as in the set framework, the while (input. hasnext () {// read a row each time. For other reading methods, see API. It is relatively simple string S = input. nextline (); system. out. println (s );}}}

 

Instance 3: read from a file
 
Import Java. io. file; import Java. io. filenotfoundexception; import Java. util. extends; public class demo {public static void main (string [] ARGs) {string Path = file. separator + "home" + file. separator + "Siu" + file. separator + "work" + file. separator + "demo.txt"; file F = new file (PATH); partition input = NULL; try {// construct a partition object from a file, an exception may occur in input = new partition (f); While (input. hasnext () {string S = input. nextline (); system. out. println (s) ;}} catch (filenotfoundexception e) {e. printstacktrace ();} finally {input. close ();}}}

Note that a file object is required before you create a sequence object from a file. You can use an anonymous object to create a sequence object.

In addition, you must capture exceptions and close file streams.

 

Printwriter class instance 4: write content to a file
 
Import Java. io. file; import Java. io. filenotfoundexception; import Java. io. printwriter; public class demo {public static void main (string [] ARGs) {string Path = file. separator + "home" + file. separator + "Siu" + file. separator + "work" + file. separator + "demo.txt"; // create a file object file = new file (PATH); printwriter P = NULL; try {// here the constructor can also upload other objects, for more information, see the API documentation P = new printwriter (File); // write a row to the file. In addition, the print () and printf () Methods P. println ("if one day I go back to the past"); p. println ("back to the original me"); p. println ("Do you think I'm good"); // refresh the stream P. flush ();} catch (filenotfoundexception e) {e. printstacktrace ();} finally {P. close ();}}}

 

Similar to printwriter, there is also a printstream class, where the printwriter is used as an example because text files are human readable.

While binary files (in byte mode) require special programs to read

Some may ask: fileoutputstream and filewriter can write files. Why do we still need the printwriter and printstream classes?

If you carefully read the API documentation, you can know that the former only supports character writing streams and byte writing streams.

It is inconvenient to refine the file, while printwriter and printstream solve this problem well and provide methods such as print ().

In addition, printwriter and printstream are directly created if no file object exists.

They will overwrite the original files, but there is no way to add them.

It's easy to solve this problem. Check the API documentation again.

Printwriter has a constructor.Printwriter (writer out), that is, the ability to pass in writer objects

Printstream has a constructor.Printstream (outputstream out), that is, an outputstream object can be passed in.

Therefore, we can write it in this way.

New printwriter (New filewriter (file, true ))

New printstream (New fileoutputstream (file, true ))

You can add data and process files more efficiently. See the following code example.

 

Instance 5: implement the data append function of printwriter
Import Java. io. file; import Java. io. filewriter; import Java. io. ioexception; import Java. io. printwriter; public class demo {public static void main (string [] ARGs) {string Path = file. separator + "home" + file. separator + "Siu" + file. separator + "work" + file. separator + "demo.txt"; // create a file object file = new file (PATH); printwriter P = NULL; try {// use filewriter to construct a printwriter object, append P = new printwriter (New filewriter (file, true); p. println ("Nima is not added"); p. flush ();} catch (ioexception e) {e. printstacktrace ();} finally {// Let's close the stream with caution, okay ^_^ p. close ();}}}

Look, this will achieve the append effect. The last line is

 

Instance 6 of Io supported by the system class: write in the system class
Import Java. io. ioexception; import Java. io. outputstream; public class demo {public static void main (string [] ARGs) {// do not forget that outputstream is the parent class of all bytes written into the stream. outputstream out = system. out; try {// write data, which can only be an array, so use the getbytes () method out. write ("Hello, bitch! \ R \ n ". getbytes ();} catch (ioexception e) {e. printstacktrace ();}}}

Note: The overwrite behavior of system. Out is confirmed here.

If you want to learn Io well, the polymorphism in the entire Io system needs to be clearly understood to be easy to learn.

 

Instance 7: read from the system class
 
Import Java. io. ioexception; import Java. io. inputstream; public class demo {public static void main (string [] ARGs) {// do not forget that inputstream is the parent class of all byte input streams. inputstream in = system. in; system. out. print ("Enter text:"); byte [] Buf = new byte [1024]; int Len = 0; try {// ensure that the input data is in the array, len record input length Len = in. read (BUF);} catch (ioexception e) {e. printstacktrace ();} // print the data system in the array in string mode. out. println ("your input is:" + new string (BUF, 0, Len ));}}

You can get the content from the keyboard and print it.

Note that the array size is 1024 bytes.

Once the input data exceeds 1024 bytes, the excess content will be truncated, so this program has limitations.

In addition, a Chinese character occupies two bytes, and the input of Chinese characters is sometimes accidentally truncated.

Believe me, every program is compiled by myself ~!!!

 

Example 8: Use bufferedreader to read the keyboard
Import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstreamreader; public class demo {public static void main (string [] ARGs) {bufferedreader B = new bufferedreader (New inputstreamreader (system. in); system. out. print ("Enter the text:"); try {string STR = B. readline (); system. out. println ("You entered:" + Str);} catch (ioexception e) {e. printstacktrace ();} // cyclic reading method/* While (true) {system. out. print ("Enter text:"); string STR = NULL; try {STR = B. readline ();} catch (ioexception e) {e. printstacktrace ();} // If the input is over, the loop ends if ("over ". equals (STR) {break;} system. out. println ("You entered:" + Str);} */try {// close the stream. If you are impatient, directly throw B. close ();} catch (ioexception e) {e. printstacktrace ();}}}

Compared with the preceding method, this method does not have to worry about the array size.

One of the most important methods for bufferedreader is Readline (), which reads a row at a time.

 

 

 

 

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.