Java Learning (2): Save keyboard entries to a specified file

Source: Internet
Author: User

Requirements: Save the keyboard entry content, when the keyboard input end, the entry ends.

1 /**2 * Save keyboard input and end with end3  * 4  * @authorXCX5 * @time June 24, 2017 PM 3:32:506  */7  Public classGetData {8 9      Public Static voidMain (string[] args)throwsIOException {TenString fileName = "D:\\java\\jj\\dd.txt";//the file path to write to OneFile File =NewFile (FileName);//To create a file object A WriteFile (file); -     } -  the     //writing to a file -      Public Static voidWriteFile (File file)throwsIOException { -         //determine if there is a path to the file -         if(File.getparentfile (). exists ()) { +             //Determine if there is this file, and if not, create it -             if(!file.exists ()) { + file.createnewfile (); A             } at             //Creating a Keyboard entry object -Scanner sc =NewScanner (system.in); -             //get keyboard entry characters and Judge -String s =sc.nextline (); -              while(!s.endswith ("End")) { -                 //creating an output byte stream inFileOutputStream fos =NewFileOutputStream (file,true); -                 //streams output bytes to a character stream toOutputStreamWriter OSW =Newoutputstreamwriter (FOS); +                 //convert characters to cached mode -BufferedWriter BW =NewBufferedWriter (OSW); the                 //Write * Bw.write (s); $                 //turn off the output streamPanax Notoginseng bw.close (); - osw.close (); the fos.close (); +                 //Accept keyboard Entry again As =sc.nextline (); the             } +  -}Else{ $System.out.println ("The file path you specified does not exist, please recheck the file path"); $         } -     } -  the}

Java Learning (2): Save keyboard entries to a specified file

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.