zoukankan      html  css  js  c++  java
  • 已知源目录路径sourceFilePath,此目录下还有多级子目录和多个文本文件(*.txt)。尝试编写一个方法,将此目录下所有的文件拷贝至另一个目录targetFilePath,并其中的文本文件修改成SQL文件(*.SQL)。

    public void copyFile(String oldPath, String newPath) throws IOException {
    	(new File(newPath)).mkdirs();
    	String[] file = new File(oldPath).list();
    	File fileTemp = null;
    	String separator = File.separator;
    	for (int i = 0; i < file.length; i++) {
    		if (oldPath.endsWith(separator)) {
    			fileTemp = new File(oldPath + file[i]);
    		} else {
    			fileTemp = new File(oldPath + separator + file[i]);
    		}
    		if (fileTemp.isFile()) {
    			FileInputStream fis = new FileInputStream(fileTemp);
    			if (file[i].endsWith(".txt")) {
    				fileTemp = new File(oldPath + separator + file[i].substring(0, file[i].length() - 4) + ".sql");
    			}
    			FileOutputStream fos = new FileOutputStream(newPath + "/" + fileTemp.getName());
    			byte[] b = new byte[1024 * 5];
    			int len;
    			while ((len = fis.read(b)) != -1) {
    				fos.write(b, 0, len);
    			}
    			fos.flush();
    			fos.close();
    			fis.close();
    		}
    		if (fileTemp.isDirectory()) {
    			this.copyFile(oldPath + "/" + file[i], newPath + "/" + file[i]);
    		}
    	}
    }
    

      

  • 相关阅读:
    Mysql update case
    phpexcel导出excel等比例缩放图片
    phpexcel错误 You tried to set a sheet active by the out of bounds index: 1解决办法
    phpexcel操作
    Java io基础
    java线程基础
    java 集合基础(适用单线程)
    java 泛型深入
    Java反射基础
    Java泛型基础
  • 原文地址:https://www.cnblogs.com/hongwei2085/p/9407480.html
Copyright © 2011-2022 走看看