文件以及文件夹处理(新建,移动,删除,复制)

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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值