IO stream:
FileOutputStream
FileInputStream
Byte stream Copy File
Binary stream with byte stream input and output
public void NewLine (): Write line delimiter based on system platform
Public String ReadLine (): reads one row of data at a time
Code manipulation "Exercise"
1 Packagecn.itcast01;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.BufferedWriter;5 ImportJava.io.File;6 ImportJava.io.FileReader;7 ImportJava.io.FileWriter;8 ImportJava.io.FilenameFilter;9 Importjava.io.IOException;Ten One A /* - * Requirements: Copy the specified type file to the specified directory in the specified directory - * the * Ideas: - * A: Package source directory - * B: Traverse the source directory for each file that matches the specified type - * C: Rename copy: + * A: Rename first, copy later; - * B: Unified renaming after copy + * A * Promotion: Replication in multi-level directory at */ - Public classFileCopyDemo2 { - Public Static voidMain (string[] args)throwsIOException { - //A: Package source directory -File File =NewFile ("D:\\java\\javacode\\day1"); - in //B: Traverse the source directory for each file that matches the specified type -file[] files = file.listfiles (NewFilenameFilter () { to @Override + Public BooleanAccept (File dir, String name) { - return NewFile (Dir,name). Isfile () && name.endswith (". Java"); the } * }); $ Panax Notoginseng //Create a directory -File DestFile =NewFile ("D:\\jad"); the if(!destfile.exists ()) { + BooleanFlag =Destfile.mkdir (); ASYSTEM.OUT.PRINTLN ("Create directory when no target directory:" +flag); the } + - //C: Renaming replication: Unified naming after Replication $ for(File filetemp:files) { $String name = Filetemp.getname ();//gets the original specified file name -String newName = Name.replace (". Java", ". Jad");//need to create target directory -File NewFile =NewFile (Destfile,newname);//Create target directory the //Copy Content -BufferedReader br =NewBufferedReader (NewFileReader (filetemp));WuyiBufferedWriter BW =NewBufferedWriter (NewFileWriter (NewFile)); the - Char[] CHS =New Char[1024]; Wu intLen = 0; - while(len = Br.read (CHS))!=-1){ AboutBw.write (chs,0, Len); $ Bw.flush (); - } - //Close Resource - bw.close (); A br.close (); + } the } -}
1 Packagecn.itcast03;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.BufferedWriter;5 ImportJava.io.File;6 ImportJava.io.FileReader;7 ImportJava.io.FileWriter;8 Importjava.io.IOException;9 Ten /** One * Traversal replication of the specified directory A * @authorItcast - * - */ the Public classFilecopydemo { - Public Static voidMain (string[] args)throwsIOException { - //encapsulates a file object -File Srcfile =NewFile ("D:\\java\\javacode\\day2"); +File DestFile =NewFile ("D:\\test"); - if(!destfile.exists ()) { + destfile.mkdirs (); A } at //gets an array of file objects under the specified directory -file[] Arrayfiles =srcfile.listfiles (); - //Traverse, and make judgments - Char[] CHS =New Char[1024]; - intLen = 0; - for(File arrayfile:arrayfiles) { in if(Arrayfile.isfile ()) { -String name = Arrayfile.getname ();//gets the original specified file name toFile Finalfile =NewFile (destfile, name);//Create target directory + //Create a replication input and output stream -BufferedReader br =NewBufferedReader (NewFileReader (Arrayfile)); theBufferedWriter BW =NewBufferedWriter (NewFileWriter (Finalfile)); * $ while(len = Br.read (CHS))!=-1){Panax NotoginsengBw.write (chs,0, Len); - Bw.flush (); the } + A //Close Resource the bw.close (); + br.close (); - } $ } $ } -}
Java Basics-Day 21st