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