자바 플 렉 스 엔 드 로 폴 더 복사

3714 단어
package notion.touch.busi;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import notion.touch.vo.ebook.FileVO;

public class FileBusi {
	
	public static String REPLACE_HOME = System.getProperty("user.dir") + "\\resource\\ebook\\" ;
	
	private static List<FileVO> files = new ArrayList<FileVO>();
	/**
	 *      
	 * @param frompath
	 * @return
	 */
	public static List<FileVO> copyDir(String frompath){
		files = new ArrayList<FileVO>();
		File from = new File(frompath); 
		if(!from.exists())
			return files;
		else{
			doCopy(from);
		}
		return files;
	}
	/**
	 *     
	 * @param frompath
	 * @return
	 */
	private static void doCopy(File from){
		if(from.isDirectory()){
			//     
			File[] children = from.listFiles();
			for(File f : children){
				
				FileVO fvo = new FileVO();
				fvo.setContent(null);
				fvo.setFilename(from.getName());
				fvo.setIsDirectory(true);
				fvo.setPath(from.getPath().replace(REPLACE_HOME, ""));
				fvo.setSize(0);
				files.add(fvo);
				
				
				doCopy(f);
			}
		}else{
			//  
			try {
				InputStream is = new FileInputStream(from);
				byte[] bts = new byte[2048];
				int len = is.read(bts);
				while(len > 0){
					len = is.read(bts);
				}
				is.close();
				
				FileVO fvo = new FileVO();
				fvo.setContent(bts);
				fvo.setFilename(from.getName());
				fvo.setIsDirectory(false);
				fvo.setPath(from.getPath().replace(REPLACE_HOME, ""));
				fvo.setSize(bts.length);
				files.add(fvo);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 *        
	 * @param dir
	 */
	public static void deleteDir(String dir){
		deleteDir(new File(dir));
	}
	/**
	 *        
	 * @param dir 
	 */
	public static void deleteDir(File from){
		System.out.println("do delete....");
		if(from.exists() && from.isDirectory()){
			doDeleteDir(from);
		}else{
			System.out.println("      ,            ");
		}
	}
	/**
	 *        
	 * @param from
	 */
	private static void doDeleteDir(File from){
		if(from.listFiles().length == 0)
			from.delete();
		for(File f : from.listFiles()){
			if(f.isDirectory()){
				doDeleteDir(f);
			}else
				f.delete();
		}
	}
	
	
	public static void main(String[] args) {
		File dir = new File("D:\\carsonsoft\\touchhome\\resources\\ebook\\SX0120121222032724447\\aaa\\ddd");
		System.out.println(dir.exists());
		if(dir.isDirectory())
			System.out.println(dir.delete());
//		deleteDir(new File("D:\\carsonsoft\\touchhome\\resources\\ebook\\SX0120121222032724447"));
	}
	
}
package notion.touch.vo.ebook;
/**
 *   VO 
 * @author ganxz
 * @date 2012-12-21
 *
 */
public class FileVO {
	
	public String id;
	public String filename;//    
	public Number size;//    
	public String path ;//    
	public byte[] content;//    
	public Boolean isDirectory;//      
	
	
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getFilename() {
		return filename;
	}
	public void setFilename(String filename) {
		this.filename = filename;
	}
	public Number getSize() {
		return size;
	}
	public void setSize(Number size) {
		this.size = size;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public byte[] getContent() {
		return content;
	}
	public void setContent(byte[] data) {
		this.content = data;
	}
	public Boolean getIsDirectory() {
		return isDirectory;
	}
	public void setIsDirectory(Boolean isDirectory) {
		this.isDirectory = isDirectory;
	}
	
	

}

좋은 웹페이지 즐겨찾기