FileCommon 文件处理公共方法

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class FileCommon extends CommonUtil
{
    /**
     * 新建文件
     * @param filePathAndName String 文件路径及名称 如c:/fqf.txt
     * @param fileContent String 文件内容
     * @return boolean
     */
    public void newFile(String filePathAndName, String fileContent)
    {
        FileWriter resultFile = null;
        try
        {
            String filePath = filePathAndName;
            filePath = filePath.toString();
            File myFilePath = new File(filePath);
            if (!myFilePath.exists())
            {
                myFilePath.createNewFile();
            }
            resultFile = new FileWriter(myFilePath);
            PrintWriter myFile = new PrintWriter(resultFile);
            String strContent = fileContent;
            myFile.println(strContent);
        }
        catch (Exception e)
        {
            Log.getLogger().error("new File exception: ", e);
        }
        finally
        {
            try
            {
                resultFile.close();
            }
            catch (IOException e)
            {
            }
        }
    }
    
    /**
     * 删除文件
     * @param filePathAndName String 文件路径及名称 如c:/fqf.txt
     * @param fileContent String
     * @return boolean
     */
    public void delFile(String filePathAndName)
    {
        try
        {
            File myDelFile = new File(filePathAndName);
            myDelFile.delete();
        }
        catch (Exception e)
        {
            Log.getLogger().error("delete File exception: ", e);
        }
    }
    
    /**
     * 创建多级目录文件夹
     * @param mkdirName
     */
    public void newFolder(String mkdirName)
    {
        try
        {
            File dirFile = new File(mkdirName);
            boolean bFile = dirFile.exists();
            if (!bFile)
            {
                bFile = dirFile.mkdirs();
                if (bFile == true)
                {
                    Log.getLogger().info("Create '" + mkdirName + "' folder successfully!");
                }
                else
                {
                    Log.getLogger().info("Disable to make '" + mkdirName
                        + "' the folder,please check the disk is full or not.");
                    System.exit(1);
                }
            }
            else
            {
                Log.getLogger().info("The folder '" + mkdirName + "' is exists!");
            }
        }
        catch (Exception e)
        {
            Log.getLogger().error("create folder exception", e);
        }
    }
    
    /**
     * 删除多级目录文件夹
     * @param[in] sPath 目录
     */
    public void delFolder(String sPath)
    {
        File oPath = new File(sPath);
        if (!oPath.exists() || !oPath.isDirectory())
        {
            return;
        }
        deleteFolder(oPath);
    }
    
    /**
     * 嵌套删除多级目录
     * @param[in] oPath 目录
     */
    private void deleteFolder(File oPath)
    {
        final File[] dirs = oPath.listFiles();
        if (dirs != null)
        {
            for (final File oSubPath : dirs)
            {
                if (oSubPath.isDirectory())
                {
                    deleteFolder(oSubPath);
                }
            }
            String[] fileList = oPath.list();
            File file = null;
            for (final String fileName : fileList)
            {
                file = new File(oPath.getPath() + File.separator + fileName);
                if (file.isFile())
                {
                    file.delete();
                }
            }
        }
        oPath.delete();
    }
    
    /**
     * 复制单个文件
     * @param oldPath String 原文件路径 如:c:/fqf.txt
     * @param newPath String 复制后路径 如:f:/fqf.txt
     * @return boolean
     */
    public void copyFile(String oldPath, String newPath)
    {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try
        {
            int byteread = 0;
            File oldfile = new File(oldPath);
            // 文件存在时
            if (oldfile.exists())
            {
                // 读入原文件
                fis = new FileInputStream(oldPath);
                fos = new FileOutputStream(newPath);
                byte[] buffer = new byte[1024];
                while ((byteread = fis.read(buffer)) != -1)
                {
                    fos.write(buffer, 0, byteread);
                }
            }
        }
        catch (Exception e)
        {
            Log.getLogger().error("copy File exception", e);
        }
        finally
        {
            try
            {
                fis.close();
                fis.close();
            }
            catch (IOException e)
            {
            }
        }
    }
    
    /**
     * 复制整个文件夹内容
     * @param oldPath String 原文件路径 如:c:/fqf
     * @param newPath String 复制后路径 如:f:/fqf/ff
     * @return boolean
     */
    public void copyFolder(String oldPath, String newPath)
    {
        try
        {
            // 如果文件夹不存在 则建立新文件夹
            (new File(newPath)).mkdirs();
            File a = new File(oldPath);
            String[] file = a.list();
            File temp = null;
            for (int i = 0; i < file.length; i++)
            {
                if (oldPath.endsWith(File.separator))
                {
                    temp = new File(oldPath + file[i]);
                }
                else
                {
                    temp = new File(oldPath + File.separator + file[i]);
                }
                if (temp.isFile())
                {
                    FileInputStream input = new FileInputStream(temp);
                    FileOutputStream output = new FileOutputStream(newPath + "/" + (temp.getName()).toString());
                    byte[] b = new byte[1024 * 5];
                    int len = 0;
                    while ((len = input.read(b)) != -1)
                    {
                        output.write(b, 0, len);
                    }
                    output.flush();
                    output.close();
                    input.close();
                }
                // 如果是子文件夹
                if (temp.isDirectory())
                {
                    Log.getLogger().info("copyFolder: " + oldPath + "/" + file[i] + " newPath: " + newPath + "/"
                        + file[i]);
                    copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
                }
            }
        }
        catch (Exception e)
        {
            Log.getLogger().error("copy Folder exception: ", e);
        }
    }
    
    /**
     * 移动文件到指定目录
     * @param oldPath String 如:c:/fqf.txt
     * @param newPath String 如:d:/fqf.txt
     */
    public void moveFile(String oldPath, String newPath)
    {
        copyFile(oldPath, newPath);
        delFile(oldPath);
    }
    
    /**
     * 移动文件到指定目录
     * @param oldPath String 如:c:/fqf.txt
     * @param newPath String 如:d:/fqf.txt
     */
    public void moveFolder(String oldPath, String newPath)
    {
        copyFolder(oldPath, newPath);
        delFolder(oldPath);
    }
    
    public static void main(String[] args)
    {
        FileCommon f = new FileCommon();
        
        f.newFolder("e:/test/src/");
        f.newFile("e:/txt.txt", "aaaaaaaaaaaa");
        f.copyFile("e:/txt.txt", "e:/test/src/txt.txt");
        f.moveFile("e:/txt.txt", "e:/test/src/txt.txt");
        f.copyFolder("e:/test", "e:/a");
        f.moveFolder("e:/test", "e:/a");
        
        f.delFolder("E:/test");
        f.delFile("e:/txt.txt");
        f.delFolder("e:/a");
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值