1. Write a program to copy all the .java files in the d:\java directory to the d:\jad directory, and change the extension of the original file from .java to .jad
package copy; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class CopyTest { public static void main(String[] args) throws Exception { File file = new File( " D:\\java " ); // Determine whether the directory exists if (!(file.exists()&& file.isDirectory())){ throw new Exception( "The directory does not exist " ); } // find the .java file under the folder File[] files=file.listFiles( new FilenameFilter() { public boolean accept(File dir, String name) { // TODO Auto-generated method stub return name.endsWith( " . java " ); } }); System.out .println ( files.length ); // Copy the file // Find the target directory File targetfile= new File( " D:\\jad " ); // Determine whether the directory exists, create if (!(targetfile) .exists()&& targetfile.isDirectory())){ targetfile.mkdirs(); } // Replace file name for (File f:files){ // 1. Use file stream to achieve file copy String targetFilename=f.getName().replace( " .java " , " .jad " ); // FileInputStream in= new FileInputStream(f); // FileOutputStream out=new FileOutputStream(new File(targetfile,targetFilename)); // copy(in,out); // 2. Use buffered streams to speed up reading and writing BufferedInputStream bis= new BufferedInputStream( new FileInputStream(f)); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(new File(targetfile,targetFilename))); CopyDemo2(bis,bos); } } /** * Use file stream to achieve file copy * @author smy * @throws Exception */ public static void copy(InputStream in,OutputStream out) throws Exception{ byte[] data = new byte[1024*10]; int len = -1; while((len = in.read(data))!=-1){ out.write(data,0,len); } in.close(); out.close(); } /** * Use buffered streams to speed up read and write efficiency * * Buffered streams are a pair of high-level streams that use them to improve reading * Write efficiency. * * Advanced Streams: Advanced Streams are used to process other streams, Advanced Streams cannot * exists independently, because there is no sense, the purpose of using advanced streams * is to address the complexity of some read and write operations. simplify us * read and write operations. * * Low-level stream: The data source is clear, and the stream that is really responsible for reading and writing data. * * Read and write operations must require low-level streams, but they do not have to be used * Advanced Streaming. * @author SMY * @throws Exception */ public static void CopyDemo2(InputStream bis,OutputStream bos) throws Exception{ int d = -1; /* * The buffer stream internally maintains a byte array as * Buffer. When we call the read() method to read a * bytes, the buffered stream will read several bytes at a time * and store in the buffer, then return the first byte, when * When we call the read method again to read a byte, * it will immediately return the second byte in the buffer, * will not happen again until all bytes have been returned * The actual read operation, reads a set of bytes into the buffer * Area. * So in essence, by reading several bytes at a time, * Reduce the number of reads and improve the read efficiency. */ while((d=bis.read())!=-1){ bos.write(d); } bis.close(); bos.close(); } }