文件以及文件夾處理(新建,移動,刪除,複製)

package DoFiles;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class DealFiles {
    
    private File folder;
    private File file;
    private File newfile ;
    private String folderpath;
    private String filepath;
    public DealFiles(String path,String filePath)
    {
        folderpath = path;
        filepath = filePath;
        folder = new File(folderpath.trim());
        file = new File(filepath.trim());
        newfile = new File("c:\\Test\\test2.txt");
    }
    
    //crate folder
    public void CreateFolder()
    {

        if(!folder.exists())
        {
            folder.mkdir();
        }
    }
    
    //crate file
    public void NewFile() throws IOException
    {
        if(!file.exists())
        {
            file.createNewFile();
        }
        
    }
    
    //delete file
    public void delFile()
    {
        file.delete();    
    }
    
    //delete the whole files under the folder
    public void delAllFile()
    {
        if(!folder.exists())
        {
        return;    
        }
        if(!folder.isDirectory())
        {
            return;
        }
        
        String [] fileList = folder.list();
        File temp;
        for(int len = 0;len<fileList.length;len++)
        {
            if(folderpath.endsWith(File.separator))
            {
                temp =new File(folderpath + fileList[len]);
            }
            else
            {
                temp = new File(folderpath + File.separator + fileList[len]);
            }
            temp.delete();
        }
    }
    
    //delete the folder
    public void delFolder()
    {
        delAllFile();
        folder.delete();
    }
    
    //copy the file
    public void copyFile() throws IOException
    {
        int bytesum=0;
        int byteread=0;
    
        if(file.exists())
        {
            InputStream IStream= new FileInputStream(file);
            FileOutputStream fos = new FileOutputStream(newfile);
        byte [] buffer= new byte[1444];
        int length;
        while((byteread=IStream.read(buffer))!=-1)
        {
            bytesum+=byteread;
            System.out.println(bytesum);
            fos.write(buffer, 0, byteread);
        }
        fos.close();
        }
    }
    
    //copy the whole folder
    public void copyFolder() throws IOException
    {
        File newFolder = new File("c:\\Test2");
        File temp;
        if (!newFolder.exists())
        {
            newFolder.mkdir();
        }
        String [] tempFileList = folder.list();
        for (int i= 0;i< tempFileList.length; i++)
        {
            if(folderpath.endsWith(File.separator))
                temp = new File(folderpath+tempFileList[i]);
            else temp = new File(folderpath+File.separator+tempFileList[i]);
            if (temp.isFile())
            {
                FileInputStream input = new FileInputStream(temp);
                FileOutputStream output= new FileOutputStream(newFolder+"\\" + tempFileList[i].trim());
                byte[] b = new byte[1024 * 5];
                int len;
                while((len=input.read(b))!=-1)
                {
                    output.write(b, 0, len);
                }
                input.close();
                output.close();
                output.flush();
            }
        }
        
        }
    
    //move the file to another path
    public void moveFile() throws IOException
    {
        copyFile();
        delFile();
    }
    
    //move the folder to another path
    public void moveFolder() throws IOException
    {
        copyFolder();
        delFolder();
    }
    
    public static void main(String [] args) throws IOException
    {
        DealFiles temp = new DealFiles("c:\\Test","c:\\Test\\test.txt");    
//        temp.CreateFolder();
//        temp.NewFile();
//        temp.delFile();
//        temp.delAllFile();
//        temp.delFolder();
//        temp.copyFile();
//        temp.copyFolder();
        temp.copyFolder();
    }
    
}




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