IO流 发表于 2018-07-08 | | 阅读次数: 复制文件需要留意的是,read会返回实际的读取数量,有可能实际的读取数量小于缓冲的大小,那么把缓冲中的数据写出到目标文件的时候,就只应该写出部分数据。123456789101112131415161718192021222324252627282930313233343536373839/*** @param srcPath 源文件* @param destPath 目标文件*/public static void copyFile(String srcPath, String destPath){ File srcFile = new File(srcPath); File destFile = new File(destPath); //缓存区,一次性读取1024字节 byte[] buffer = new byte[1024]; try ( FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile); ){ while(true){ //实际读取的长度是 actuallyReaded,有可能小于1024 int actuallyReaded = fis.read(buffer); //-1表示没有可读的内容了 if(-1==actuallyReaded) break; fos.write(buffer, 0, actuallyReaded); fos.flush(); } } catch (IOException e) { e.printStackTrace(); } } /*** @param srcPath 源文件夹* @param destPath 目标文件夹*/public static void copyFolder(String srcPath, String destPath){}public static void main(String[] args) { copyFile("C:\\Users\\12905\\Desktop\\xxx\\2.txt", "C:\\Users\\12905\\Desktop\\xxx\\3.txt");} 复制文件夹123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172/*** @param srcPath 源文件* @param destPath 目标文件*/public static void copyFile(String srcPath, String destPath){ File srcFile = new File(srcPath); File destFile = new File(destPath); //缓存区,一次性读取1024字节 byte[] buffer = new byte[1024]; try ( FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile); ){ while(true){ //实际读取的长度是 actuallyReaded,有可能小于1024 int actuallyReaded = fis.read(buffer); //-1表示没有可读的内容了 if(-1==actuallyReaded) break; fos.write(buffer, 0, actuallyReaded); fos.flush(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }} /*** @param srcPath 源文件夹* @param destPath 目标文件夹*/public static void copyFolder(String srcPath, String destPath){ File srcFolder = new File(srcPath); File destFolder = new File(destPath); //源文件夹不存在 if(!srcFolder.exists()) return; //源文件夹不是一个文件夹 if(!srcFolder.isDirectory()) return; //目标文件夹是一个文件 if(destFolder.isFile()) return; //目标文件夹不存在 if(!destFolder.exists()) destFolder.mkdirs(); //遍历源文件夹 File[] files= srcFolder.listFiles(); for (File srcFile : files) { //如果是文件,就复制 if(srcFile.isFile()){ File newDestFile = new File(destFolder,srcFile.getName()); copyFile(srcFile.getAbsolutePath(), newDestFile.getAbsolutePath()); } //如果是文件夹,就递归 if(srcFile.isDirectory()){ File newDestFolder = new File(destFolder,srcFile.getName()); copyFolder(srcFile.getAbsolutePath(),newDestFolder.getAbsolutePath()); } }}public static void main(String[] args) { copyFolder("C:\\Users\\12905\\Desktop\\xxx", "C:\\Users\\12905\\Desktop\\yyy");} 查找文件内容12345678910111213141516171819202122232425262728293031323334353637/** * @param file 查找的目录 * @param search 查找的字符串 */public static void search(File file, String search) { if (file.isFile()) { if(file.getName().toLowerCase().endsWith(".java")){ String fileContent = readFileConent(file); if(fileContent.contains(search)){ System.out.printf("找到子目标字符串%s,在文件:%s%n",search,file); } } } if (file.isDirectory()) { File[] fs = file.listFiles(); for (File f : fs) { search(f, search); } }} public static String readFileConent(File file){ try (FileReader fr = new FileReader(file)) { char[] all = new char[(int) file.length()]; fr.read(all); return new String(all); } catch (IOException e) { e.printStackTrace(); return null; }}public static void main(String[] args) { File folder =new File("e:\\project"); search(folder,"Magic");} 流关系图 Your support will encourage me to continue to create! Donate WeChat Pay Alipay