1 Packagetest;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.File;5 ImportJava.io.FileOutputStream;6 ImportJava.io.FileReader;7 Importjava.io.IOException;8 Importjava.io.UnsupportedEncodingException;9 ImportJava.text.SimpleDateFormat;Ten Importjava.util.Date; One A Public classFileOperation { - /** - * Generate Files the * @paramFileName - */ - Public Static BooleancreateFile (File fileName) { - if(!filename.exists ()) { + Try { - filename.createnewfile (); +}Catch(Exception e) { A //TODO auto-generated Catch block at e.printstacktrace (); - } - } - return true; - } - in /** - * @description Read the file to * @throwsIOException + */ - Public StaticString readtxtfile (File fileName)throwsioexception{ theString result =NULL; *FileReader FileReader =NULL; $BufferedReader BufferedReader =NULL;Panax NotoginsengFileReader =NewFileReader (fileName); -BufferedReader =NewBufferedReader (filereader); the +String Read =NULL; A intCount = 0; the while(read = Bufferedreader.readline ())! =NULL){ +Result = result + count + read + "\ r \ n"; -count++; $ } $ - if(BufferedReader! =NULL){ - bufferedreader.close (); the } - Wuyi if(FileReader! =NULL){ the filereader.close (); - } Wu -System.out.println ("¶áè¡îä¼þµääúèýê磺" + "\ r \ n" +result); About returnresult; $ } - - /** - * @description Write files A * @paramargs + * @throwsunsupportedencodingexception the * @throwsIOException - */ $ Public Static BooleanWritetxtfile (String content,file fileName)throwsunsupportedencodingexception, ioexception{ theFileOutputStream o =NULL; theo =NewFileOutputStream (fileName); theO.write (Content.getbytes ("UTF-8")); the o.close (); - return true; in } the the /** About * @description Unit Test the * @throwsIOException the */ the Public Static voidMain (string[] args)throwsIOException { +File File =NewFile ("D:/hello.txt"); -Date Date =NewDate (); theSimpleDateFormat Day =NewSimpleDateFormat ("Yyyymmddhhmmss");BayiString Datename =Day.format (date); theFile CopyFile =NewFile ("d:/" + datename + ". txt"); theString content =readtxtfile (file); - CreateFile (copyFile); - Writetxtfile (content,copyfile); the } the}
Tool classes for file reading and writing