Two examples of program logs are programmed in the console to receive input characters and redirect output streams

Source: Internet
Author: User

1. Console receiving characters

The main principle is through callback. SC = new centers (System. in); Create a scanner, and then use String line = SC. line (); get the user, and finally "+ line. length () + "read digits

Import java. util. extends; public class InputCode {public static void main (String [] args) {Using SC = new using (System. in); // create a Scanner System. out. println ("Enter your ID card number:"); // you are prompted to enter String line = SC. nextLine (); // obtain the user's input text System. out. println ("your ID card was" + line. length () + "digits"); // print }}

650) this. width = 650; "src =" http://img1.51cto.com/attachment/201309/222724633.jpg "title =" jg9ym4(j@yr'upde.pdf (cd000012.jpg "alt =" 222724633.jpg"/>


2. Redirect the output stream to implement program logs

Package com. lixiyu; import java. io. fileNotFoundException; import java. io. printStream; public class RedirectOutputStream {public static void main (String [] args) {try {PrintStream out = System. out; // Save the original output stream PrintStream a = new PrintStream (". /log.txt "); // create the file output stream System. setOut (a); // set to use the new output stream int age = 18; // define an integer System. out. println ("age defined successfully, initial value: 18"); String sex = "female"; System. out. println ("Gender defined successfully, initial value: female"); // integrate the two variables S Tring info = "this is a" + sex + "child. It should be" + age + "years old. "; System. out. println ("the two variables are integrated into the info string variable, and the result is:" + info); System. setOut (out); // restore the original output stream System. out. println ("the program is running successfully. Check the log file. ");} Catch (FileNotFoundException e) {e. printStackTrace ();}}}


In this example, a log.txt file is generated in the folder, showing:

Age defined successfully. Initial Value: 18

Gender defined successfully. Initial Value: Female

Integrate the two variables into the info string variable. The result is: this is a girl, and it should be 18 years old.


Done!


This article is from the "ToBeContinued" blog, please be sure to keep this source http://leexy.blog.51cto.com/4136883/1302364

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.