File文件处理下载复制

File文件处理下载复制,第1张

package com.sdmc.util.mangoDownJar;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;

import org.slf4j.LoggerFactory;
import org.slf4j.Logger;


public class MyFileUtil {

	public static ArrayList mListFile;
	private int mCopyChunkSize;
	
	private static Logger logger=LoggerFactory.getLogger(MyFileUtil.class);
	
	public MyFileUtil() {
		mListFile = new ArrayList();
		mListFile.clear();
		
		mCopyChunkSize = 1024 * 1024 * 10;
	}

	
	public void setCopyChunkSize(int size) {
		mCopyChunkSize = size;
	}
	
	
	public static void mkDir(String path) {
		File catchDir = new File(path);
		if (!catchDir.exists()) {
			catchDir.mkdirs();
			catchDir.setWritable(true, false);
			catchDir.setReadable(true, false);
		}
	}

	
	public boolean copyFile(File src, File dest, boolean isdeletesrc) {
		if (!src.exists()) {
			logger.error(src.getAbsolutePath() + " is not exist.");
			return false;
		}
		
		long srcFileSize = src.length();
		logger.error("copy size(src):" + Long.toString(srcFileSize));
		
		long destFileSize = 0;
		if(dest.exists()) {
			destFileSize = dest.length();
			logger.error("copy size(dest):" + Long.toString(destFileSize));
			if(destFileSize > srcFileSize) {
				logger.error("destFileSize > srcFileSize,delete dest file and repeat copy.");
				dest.delete();
				destFileSize = 0;
			} else if (destFileSize == srcFileSize) {
				logger.error("destFileSize = srcFileSize,copy success.");
				if(isdeletesrc) {
					logger.error("delete file " + src.getAbsolutePath());
					src.delete();
				}
				return true;
			}
		}
		
		if (!dest.exists()) {
			try {
				if (!dest.getParentFile().exists())
					dest.getParentFile().mkdirs();
				dest.createNewFile();
			} catch (IOException e) {
				logger.error("create " + dest.getAbsolutePath() + "fail.");
				return false;
			}
		}
		
		try {
			RandomAccessFile accessReadFile = new RandomAccessFile(src, "r");
			RandomAccessFile accessWriteFile = new RandomAccessFile(dest, "rw");
			if(destFileSize > 0) {
				accessReadFile.seek(destFileSize);
				accessWriteFile.seek(destFileSize);
			}
			logger.error("start Read:" + accessReadFile.getFilePointer());
			logger.error("start Write:" + accessWriteFile.getFilePointer());
			
			byte[] readByte = new byte[mCopyChunkSize];
			int readSize = 0;
			while((readSize = accessReadFile.read(readByte)) > 0) {
				accessWriteFile.write(readByte, 0, readSize);
			}
			accessReadFile.close();
			accessWriteFile.close();
			
			destFileSize = dest.length();
			if(destFileSize == srcFileSize) {
				logger.error("-->copy file " + src.getAbsolutePath() + " success.");
				if(isdeletesrc) {
					logger.error("-->delete file " + src.getAbsolutePath());
					src.delete();
				}
				return true;
			} else {
				logger.error("-->copy file " + src.getAbsolutePath() + " fail. length not ==");
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return false;
	}
	
	
	public boolean copyDir(File src, File dest, boolean isdeletesrc) {

		if (src.isDirectory()) {
			mListFile.clear();
			getFileList(src.getAbsolutePath());
			
			ArrayList mDestListFile = new ArrayList();
			mDestListFile.clear();

			for (String str : mListFile) {
				mDestListFile.add(str.replace(src.getAbsolutePath(), dest.getAbsolutePath()));
			}

			for (int index = 0; index < mListFile.size(); ++index) {
				copyFile(new File(mListFile.get(index)), new File(mDestListFile.get(index)), isdeletesrc);
			}
			return true;
		} else {
			return copyFile(src, dest, isdeletesrc);
		}
	}

	
	public static boolean rename(String oldname, String newname) {
		
		File srcFile = new File(oldname);
		if(!srcFile.exists()) {
			logger.error(oldname + " not exist.");
			return false;
		}
		File destFile = new File(newname);
		
		return srcFile.renameTo(destFile);
	}
	
	
	public void getFileList(String path) {
		File root = new File(path);
		if (root.isDirectory()) {

			File[] list = root.listFiles();
			for (File file : list) {
				if (file.isDirectory()) {
					getFileList(file.getAbsolutePath());
				} else {
					mListFile.add(file.getAbsolutePath());
				}
			}
		} else {
			mListFile.add(root.getPath());
		}
	}
	
	
	public static String getFileExtension(String path) {
		if ((path == null) || (path.isEmpty()))
			return "";

		int index = path.lastIndexOf(".");
		if (index < 0)
			return "";
		
		return path.substring(index);
	}
	
	
	public static String getFileName(String path, String split) {
		int index = path.lastIndexOf(split);
		if (index < 0)
			return "";

		return path.substring(path.lastIndexOf(split) + 1);
	}
	
	
	public static boolean deleteDir(File dir) {
		if (dir.isDirectory()) {
			String[] children = dir.list();
			for (int i = 0; i < children.length; i++) {
				boolean success = deleteDir(new File(dir, children[i]));
				if (!success) {
					return false;
				}
			}
		}
		return dir.delete();
	}
	
	public static boolean isImageFile(String extension) {
		if ((extension.compareToIgnoreCase(".jpg") != 0)
				&& (extension.compareToIgnoreCase(".jpeg") != 0)
				&& (extension.compareToIgnoreCase(".png") != 0)
				&& (extension.compareToIgnoreCase(".gif") != 0) 
				&& (extension.compareToIgnoreCase(".bmp") != 0)) {
			return false;
		}
		return true;
	}
	
	public static class FileInfo {
		public String mFileParentDir;
		public String mAbsolutePath;
		public String mFileName;
		public String mFileExtension;
		public long mFileSize;
	}
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/919711.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存