JAVA文件工具类之——文件复制、内容获取、目录获取、文件转换

//1.拷贝文件
//2.复制文件
//3.获取文件夹下所有目录路径
//4.获取指定文件内容
//5.文件转字节流信息
        /**
	 * 拷贝文件
	 * 
	 * @param src
	 * @param dist
	 * @return
	 */
	public static boolean copy(File src, File dist) {
		boolean isSuccess = false;
		if (src.exists()) {
			if (src.isFile()) {
				isSuccess = copyFile(src, dist);
			} else {
				if (dist.getPath().indexOf(src.getPath()) != -1) {
					return false;
				}
				dist.mkdirs();
				File[] fs = src.listFiles();
				for (File f : fs) {
					copy(f,
							new File(dist.getPath() + File.separator
									+ f.getName()));
				}
				isSuccess = true;
			}
		}
		return isSuccess;
	}

	/**
	 * 复制文件
	 * 
	 * @param src
	 * @param dist
	 * @return
	 */
	public static boolean copyFile(File src, File dist) {
		if ((src.exists()) && (src.isFile())) {
			try {
				writeFile(new FileInputStream(src), dist);
				return true;
			} catch (FileNotFoundException e) {
				logger.warn("复制文件时,找不到文件", e);
			}
		}
		return false;
	}
       
       /**
     * @Title: getFilePaths  
     * @Description:获取文件夹下所有目录路径  
     * @param path
     * @param filePathlist    
     * @return void    返回类型  
     * @throws
     */
    public static void getFilePaths(String path,List filePathlist) {
        File dir = new File(path);
        File[] files = dir.listFiles();
        if (files == null)
            return;
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                getFilePaths(files[i].getAbsolutePath(),filePathlist);
            } else {
                String strFileName = files[i].getAbsolutePath().toLowerCase();
                if(!filePathlist.contains(files[i].getParent())){
                    filePathlist.add(files[i].getParent());
                }
            }
        }
    }
    /**
     * 获取指定文件内容
     * 
     * @param fileName
     * @return byte[] 文件内容
     */
    public static byte[] getFileData(String fileName) {
        byte[] buff = null;
        FileInputStream inStm = null;
        try {
            File file = new File(fileName);
            int len = (int) file.length();
            buff = new byte[len];
            inStm = new FileInputStream(file);
            inStm.read(buff);
            inStm.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (inStm != null)
                    inStm.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return buff;
    }
    /**
     * 文件转字节流信息
     * 
     * @param file
     * @return
     */
    public static byte[] readFileToByteArray(File file) {
        try {
            return org.apache.commons.io.FileUtils.readFileToByteArray(file);
        } catch (IOException e) {
            logger.warn("读入文件时,io异常", e);
        }
        return null;
    }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章