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;
    }


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