<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    孤燈野火
    暢想的天空
    posts - 2,comments - 4,trackbacks - 0
    文件操作類
    package fileOperation;

    import java.io.File;
    /**
     * 查看,修改文件或目錄的屬性
     * 
    @author wakin
     *
     
    */
    public class Attribute {
        
    /**
         * 查看路徑名所表示文件或目錄的屬性。
         * 
    @param fileName 路徑名
         
    */
        
    public void lookAttribute(String fileName) {
            
    boolean canRead;
            
    boolean canWrite;
            
    boolean canExecute;
            File file 
    = new File(fileName);
            
    if(!file.exists())
                
    throw new RuntimeException("File:"+fileName+"is not exist");
            canRead 
    = file.canRead();
            canWrite 
    = file.canWrite();
            canExecute 
    = file.canExecute();
            System.out.println(
    "Can read:"+canRead+"    Can write:"+canWrite+"    Can Execute:"+canExecute);
        }
        
    /**
         * 設置路徑名所表示的文件或目錄的的屬性。?部分功能可能在windows下無效。
         * 
    @param fileName 路徑名
         * 
    @param readable 是否可讀
         * 
    @param writable 是否可寫
         * 
    @param executable 是否可執行
         * 
    @param ownerOnly  是否用戶獨享
         * 
    @return 屬性設置成功,返回true,否則返回false
         
    */
        
    public boolean setAttribute(
                String fileName,
                
    boolean readable,
                
    boolean writable,
                
    boolean executable,
                
    boolean ownerOnly)
        {
            
    boolean isDone = false;
            File file 
    = new File(fileName);
            isDone 
    = file.setReadable(readable, ownerOnly) 
                    
    && file.setWritable(writable, ownerOnly)
                    
    && file.setExecutable(executable, ownerOnly);
            
    return isDone;
        }
    }


    package fileOperation;

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;

    /**
     * 復制文件和文件夾工具類,能判斷源文件不存在,源文件不可讀,目標文件已經存在,
     * 目標路徑不存在,目標路徑不可寫等情況 
     * 
    @author wakin
     *
     
    */
    public class Copy 
    {

        
    /**根據源路徑名和目標路徑名復制文件。
         * 
         * 
    @param source_name 源路徑名
         * 
    @param dest_name 目標路徑名
         * 
    @param type  值為判斷如果目標路徑存在是否覆蓋,1為覆蓋舊的文件,2為不覆蓋,操作取消。 
         * 
    @return 當復制成功時返回1, 當目標文件存在且type值為2時返回2,其他情況返回0
         * 
    @throws IOException  發生I/O錯誤
         
    */
        
    public int copyFile(
                String source_name,
                String dest_name,
                
    int type) throws IOException {
            
    int result = 0;
            
    int byte_read;
            
    byte [] buffer;
            File source_file 
    = new File(source_name);
            File dest_file 
    = new File(dest_name);
            FileInputStream source 
    = null;
            BufferedInputStream bis 
    = null;
            BufferedOutputStream bos 
    = null;
            FileOutputStream dest 
    = null;
            
    try {
                
    if(!source_file.exists() || !source_file.isFile()) //不存在
                    throw new RuntimeException("FileCopy: no such source file:"+source_name);
                
    if(!source_file.canRead()) //不可讀
                    throw new RuntimeException("FileCopy: source file"+source_name
                            
    +"is unreadable");
                
    if(dest_file.exists()) {
                    
    if(dest_file.isFile()) {
                        
    if(type==1) { 
                            dest_file.delete();   
    //覆蓋
                            result = 1 ;
                        }
                        
    else if(type==2) {
                            result 
    = 2;
                            
    return result;    //不覆蓋 ,程序返回。
                        }
                    }
                    
    else
                        
    throw new RuntimeException("FileCopy: destination"+dest_name
                                
    +"is not a file.");
                }
                
    else {
                    File parentDir 
    = new File(dest_file.getParent());  //獲得目錄信息
                    if(!parentDir.exists()) {
                        
    throw new RuntimeException("FileCopy: destination"+dest_name
                                
    +"directory doesn't exist.");    //目錄不存在
                    }
                    
    if(!parentDir.canWrite())
                        
    throw new RuntimeException("FileCopy: destination"+dest_name
                                
    +"is unwriteable.");            //目錄不可寫
                }
                
                
    //開始復制文件
                
    //輸入流
                source = new FileInputStream(source_file);
                bis 
    = new BufferedInputStream(source);
                
    //輸出流
                dest = new FileOutputStream(dest_file);
                bos 
    = new BufferedOutputStream(dest);
                buffer 
    = new byte[1024*5];
                
    while((byte_read=bis.read(buffer))!=-1) {
                    bos.write(buffer, 
    0, byte_read);
                }
                result 
    = 1;
            } 
    catch (IOException e) {
                
    // TODO: handle exception
                e.printStackTrace();
            } 
    finally {
                
    if(source!=null){
                    bis.close();
                    source.close();
                }
                
    if(dest!=null) {
                    bos.flush();
                    bos.close();
                    dest.flush();
                    dest.close();
                }
            }
            
    return result;
            
        }
        
        
    /**根據源路徑名和目標路徑名復制目錄。
         * 復制目錄以復制文件為基礎,通過遞歸復制子目錄實現。
         * 
    @param source_name 源路徑名
         * 
    @param dest_name 目標路徑名
         * 
    @param type 值為判斷如果目標路徑存在是否覆蓋,1為覆蓋舊的目錄,2為不覆蓋,操作取消。
         * 
    @return 當復制成功時返回1, 當目標目錄存在且type值為2時返回2,其他情況返回0
         * 
    @throws IOException  發生I/O錯誤
         
    */
        
    public int copyDirectory(
                String source_name,
                String dest_name,
                
    int type
                ) 
    throws IOException {
            File source_file 
    = new File(source_name);
            File dest_file 
    = new File(dest_name);
            
    int result = 0;
            Delete del 
    = new Delete();         //用于刪除目錄文件
            if(!source_file.exists()||source_file.isFile())  //不存在
                throw new RuntimeException("DirCopy: no such dir"+source_name);
            
    if(!source_file.canRead()) //不可讀
                throw new RuntimeException("DirCopy: source file"+source_name
                        
    +"is unreadable");
            
    if(dest_file.exists()) {
                
    if(type==1) {
                    del.deleteDir(dest_name);   
    //覆蓋
                    result = 1;
                }
                
    if(type==2) {
                    result 
    = 2;                 //不覆蓋
                    return result;
                }
            }
            
    if(!dest_file.exists()) {
                
    new File(dest_name).mkdirs(); //創建目標目錄
                File[] fileList = source_file.listFiles();
                
    for(int i=0;i<fileList.length;i++){
                    System.out.println(fileList[i].getName());
                    
    if(fileList[i].isFile()){
                        
    //用copyFile函數復制文件
                        this.copyFile(
                                source_name
    +"/"+fileList[i].getName(), 
                                dest_name
    +"/"+fileList[i].getName(), 
                                type);
                    }
                    
    else if(fileList[i].isDirectory()){
                        
    //遞歸
                        copyDirectory(
                                source_name
    +"/"+fileList[i].getName(), 
                                dest_name
    +"/"+fileList[i].getName(), type);
                    }
                }
                result 
    = 1;
                
            }
            
            
    return result;
        }
    }
    package fileOperation;

    import java.io.File;

    /**創建一個新的文件或目錄
     * 
    @author wakin
     *
     
    */
    public class Create 
    {
        
    /**
         * 根據路徑名創建文件
         * 
    @param filePath 路徑名
         * 
    @return 當創建文件成功后,返回true,否則返回false.
         * 
         
    */
        
    public boolean createFile(String filePath) {
            
    boolean isDone = false;
            File file 
    = new File(filePath);
            
    if(file.exists())
                
    throw new RuntimeException("File: "+filePath+" is already exist");
            
    try {
                isDone 
    = file.createNewFile();
            } 
    catch (Exception e) {
                
    // TODO: handle exception
                e.printStackTrace();
            }
            
    return isDone;
            
        }
        
        
    /**
         * 根據路徑名創建目錄
         * 
    @param filePath 路徑名
         * 
    @return 當創建目錄成功后,返回true,否則返回false.
         
    */
        
    public boolean createDir(String filePath) {
            
    boolean isDone = false;
            File file 
    = new File(filePath);
            
    if(file.exists())
                
    throw new RuntimeException("FileDirectory: "+filePath+" is already exist");
            isDone 
    = file.mkdirs();
            
    return isDone;
        }

    }
    package fileOperation;

    import java.io.File;
    import java.io.IOException;

    /**
     * 刪除一個文件或目錄
     * 
    @author wakin
     *
     
    */
    public class Delete 
    {

        
    /**
         * 刪除路徑名所代表的文件。
         * 
    @param filePath 路徑名
         * 
    @return 當文件成功刪除,返回true,否則返回false.
         
    */
        
    public boolean deleteFile(String filePath) throws IOException{
            File file 
    = new File(filePath);
            
    if(file.exists()) {
                file.delete();
                
    //System.out.println(filePath+"文件已刪除.");
                return true;
            }
            
    else {
                
    //System.out.println("邏輯錯誤:"+filePath+"文件不存在.");
                return false;
            }
        }
        
        
    /**
         * 刪除路徑名所代表的目錄
         * 
    @param filePath 路徑名
         * 
    @return 當目錄成功刪除,返回true,否則返回false.
         
    */
        
    public boolean deleteDir(String filePath) throws IOException{
            
    boolean isDone = false;
            File file 
    = new File(filePath);
            
    //判斷是文件還是目錄
            if(file.exists()&&file.isDirectory()){
                
    if(file.listFiles().length==0){
                    file.delete();
                    isDone 
    = true;
                }
                
    else {
                    File [] delFiles 
    = file.listFiles();
                    
    for(int i=0;i<delFiles.length;i++){
                        
    if(delFiles[i].isDirectory()){
                            deleteDir(delFiles[i].getAbsolutePath()); 
    //遞歸調用deleteDir函數
                        }
                        
    else {
                            delFiles[i].delete();
                        }
                    }
                }
                
    //刪除最后剩下的目錄名。
                deleteDir(filePath);
                isDone 
    = true;
            }
            
    else
                
    return false;
            
    return isDone;
        }

    }
    package fileOperation;

    import java.io.File;
    import java.io.IOException;
    /**
     * 移動文件/目錄,利用Delete類和Copy類來實現。
     * 
    @author wakin
     *
     
    */
    public class Move {
        
    /**
         * 利用Copy類的函數和Delete類來完成移動文件/目錄的操作。
         * 
    @param source_name 源路徑名
         * 
    @param dest_name   目標路徑名
         * 
    @param type type  值為判斷如果目標路徑存在是否覆蓋,1為覆蓋舊的文件/目錄,2為不覆蓋操作取消。 
         * 
    @return 當移動成功后返回1,當目標目錄存在且type值為2時返回2,其他情況返回0
         * 
    @throws IOException  發生I/O錯誤
         
    */
        
    public int move(String source_name,String dest_name,int type) throws IOException{
            
    int result = 0;
            Copy copy 
    = new Copy();
            Delete delete 
    = new Delete();
            File source_file 
    = new File(source_name);
            
    //File dest_file = new File(dest_name);
            if(!source_file.exists())
                
    throw new RuntimeException("FileMove: no such source file:"+source_name);
            
    if(source_file.isFile()){
                result 
    = copy.copyFile(source_name, dest_name, type); //調用Copy類的copyFile函數
                if(result ==1)
                    delete.deleteFile(source_name);   
    //調用Delete類的deleteFile函數刪除源文件
            }
            
    else {
                result 
    = copy.copyDirectory(source_name, dest_name, type);//調用Copy類的copyDirectory函數
                if(result == 1)
                    delete.deleteDir(source_name);    
    //調用Delete類的deleteDir函數刪除源目錄
            }
            
    return result;
        }
    }
    package fileOperation;

    import java.io.File;
    import java.io.IOException;
    /**
     * 計算文件或目錄的空間大小。
     * 
    @author wakin
     *
     
    */
    public class Size {
        
    /**
         * 計算路徑名代表的文件或目錄所占空間的大小。
         * 
    @param fileName 路徑名
         * 
    @return 所占字節數
         * 
    @throws IOException 發生I/O錯誤
         
    */
        
    public static long getSize(String fileName) throws IOException {
            
    long result = 0;
            File file 
    = new File(fileName);
            
    if(!file.exists())
                
    throw new RuntimeException("No such source file:"+fileName);
            
    if(file.isFile()){
                
    return file.length();
            }
            
    else {
                String [] FileList 
    = file.list();
                
    for(int i =0;i<FileList.length;i++) {
                    result 
    += getSize(fileName+"/"+FileList[i]);   //迭代
                }
            }
            
    return result;
        }
    }







    posted on 2009-05-15 01:01 孤飛燕 閱讀(1333) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 在线观看免费高清视频| 亚洲大香人伊一本线| 日本一道综合久久aⅴ免费| 久久免费精彩视频| 四虎精品免费永久免费视频| 亚洲一区二区三区91| 亚洲AV无码专区国产乱码电影 | 免费观看国产小粉嫩喷水| 中文字幕亚洲免费无线观看日本| 一级午夜免费视频| 欧美日韩亚洲精品| 亚洲欧洲日韩极速播放| 亚洲福利秒拍一区二区| 亚洲国产成人久久精品影视| 国产亚洲美女精品久久久2020 | 亚洲精品第一国产综合亚AV| 亚洲成aⅴ人在线观看| 亚洲美女精品视频| 亚洲一区二区成人| 久久久无码精品亚洲日韩按摩| 亚洲精品无码久久久久sm| 亚洲男人第一无码aⅴ网站| 亚洲成aⅴ人片久青草影院| 国产高清免费观看| 国产色爽女小说免费看| 麻豆国产人免费人成免费视频 | 亚洲中文字幕久久精品无码A| 亚洲人成网站18禁止久久影院 | 人妻无码久久一区二区三区免费 | 亚洲精品视频免费看| 亚洲人成网站在线播放影院在线 | 无码日韩精品一区二区三区免费 | 久久亚洲国产精品| 亚洲第一福利视频| 久久久久亚洲AV成人片| 亚洲网站在线免费观看| 亚洲美女精品视频| 亚洲熟妇AV乱码在线观看| MM1313亚洲国产精品| 综合偷自拍亚洲乱中文字幕| 无遮挡呻吟娇喘视频免费播放 |