JAVA中文件的操作

 Ø 
 
方法名 描述   
   
makeDir(String dirName) 创建本地目录,支持三级以上目录的创建   
On Entry: dirName: 目录名称包括目录路径如(D://test//Public
//JR)   
On Exit: return true/false 返回成功/失败   
makeRemoteDir(HttpServletRequest request,String dirs) 创建web目录,支持三级以上目录的创建   
On Entry: request:Http回应request类
dirs:Web路径 /相对路径 例如/Document/Contract/   
On Exit: return true/false 返回成功/失败   
deleteDirectory(String fullDirName) 删除本地目录,将删除该目录下所有文件夹以及文件   
On Entry: dirName 目录所在路径(绝对路径 如: D://test//Public
//JR)   
On Exit: return true/false 返回成功/失败   
deleteRemoteDir(HttpServletRequest request,String filePath) 删除Web目录将删除该目录下所有文件夹以及文件   
On Entry: request:Http回应request类
filePath:Web路径 /相对路径例如/Document/Contract/   
On Exit: return true/false 返回成功/失败   
moveDirectory(String sourceDir,String desDir) 将本地一个目录中文件移动本地另一个目录中.包括所有文件夹和文件
目标目录不存在程序可以自己创建   
On Entry: SourDir: 源目录
DesDir: 目标目录   
On Exit: return true/false 返回成功/失败   
moveDirectory(String sourceDir,String desDir,String dirName) 将本地一个目录中文件移动本地另一个目录的指定文件中.包括所有文件夹和文件
目标目录不存在程序可以自己创建
此方法与上一方法moveDirectory(String sourceDir,String desDir)类似.   
On Entry: SourDir: 源目录
DesDir: 目标目录
DirName: 目标目录指定文件夹的名称   
On Exit: return true/false 返回成功/失败   
createFile(String fullFileName,String txt) 创建本地文件如创建.txt,.doc 文件(不支持a.c.d.x.txt文件的创建)   
On Entry: fullFileName 文件路径+文件名称 如D://test//Public//test.txt
txt 文件内容   
On Exit: return true/false 返回成功/失败   
deleteFile(String fullFileName) 删除本地文件(不支持通配符删除)   
On Entry: FullFileName: 文件绝对路径+文件名称 如:D://test//test.jsp    
On Exit: return true/false 返回成功/失败   
deleteRemoteFile(HttpServletRequest request,String filePath) 删除web文件(不支持通配符删除)   
On Entry: request:Http回应request类
filePath:文件路径 /相对路径例如/Document/Contract/Test.xml   
On Exit: return true/false 返回成功/失败   
getFileExtName(String fullFileName) 取得文件扩展名   
On Entry: FullFileName: 文件路径+文件名称 文件路径+文件名称或者文件名 如:D://test//test.jsp 或者/test/test.jsp 或者test.jsp   
On Exit: Return String 扩展名   
getFileNoExtName(String fullFileName) 取得文件名称不含扩展名   
On Entry: FullFileName: 文件路径+文件名称或者文件名 如:D://test//test.jsp 或者/test/test.jsp 或者test.jsp   
On Exit: Return String 文件名   
getFile (String path) 取得文件名称含扩展名   
On Entry: path: 文件路径+文件名称或者文件名 如:D://test//test.jsp 或者/test/test.jsp   
On Exit: Return String 文件名   
isExist(String fullFileName) 判断本地目录或者文件是否存在   
On Entry: FullFileName: 文件路径+文件名称或者文件目录(绝对路径)   
On Exit: return true/false 返回成功/失败   
isRemoteExist(HttpServletRequest request,String fullFileName) 判断web目录或者文件是否存在   
On Entry: FullFileName: 文件路径+文件名称或者web目录(相对路径)   
On Exit: return true/false 返回成功/失败   
reName(String oldFileName,String newFileName) 本地文件更名   
On Entry: OldFileName: 文件路径+文件名称 如:D://test//test.jsp
NewFileName: 更改后文件名(只能是文件名不能带文件路径)   
On Exit: return true/false 返回成功/失败   
reNameRemoteFile(HttpServletRequest request ,String oldFileName,String newFileName) web目录文件更名   
On Entry: OldFileName: 文件路径+文件名称 如:/test/test.jsp
NewFileName: 更改后文件名(只能是文件名不能带文件路径)   
On Exit: return true/false 返回成功/失败   
moveFile(String src,String des) 源文件移动到新的路径下des路径不存在方法可以自己创建. des为目标文件路径.此处des必须是文件路径名,并且必须要以”//”结尾.
   
On Entry: Src: 源文件路径及文件名
Des: 目的文件路径及文件名   
On Exit: return true/false 返回成功/失败 

import java.io.*;
import javax.servlet.http.HttpServletRequest;
import smart.jrsoft.util.StringUtils;

public class FileOperation
{

    public FileOperation()
    {
    }

    public boolean makeDir(String dirs)
        throws Exception
    {
        boolean result = false;
        try
        {
            File fi = new File(dirs);
            result = fi.mkdirs();
        }
        catch(Exception e)
        {
            result = false;
            System.err.println(e.getMessage());
        }
        return result;
    }

    public boolean makeRemoteDir(HttpServletRequest request, String dirs)
        throws Exception
    {
        boolean result = false;
        if(dirs != null)
        {
            String pathString = "";
            pathString = request.getRealPath("");
            pathString = pathString.replace('//', '/');
            pathString = pathString + dirs;
            try
            {
                File fi = new File(pathString);
                result = fi.mkdirs();
            }
            catch(Exception e)
            {
                result = false;
                System.err.println(e.getMessage());
            }
        }
        return result;
    }

    public boolean deleteDirectory(String fullDirName)
    {
        boolean result = false;
        int len = 0;
        int i = 0;
        try
        {
            File Dire = new File(fullDirName);
            if(!Dire.exists())
                result = false;
            if(Dire.isFile())
                result = Dire.delete();
            File fi[] = Dire.listFiles();
            len = fi.length;
            if(len == 0)
                result = Dire.delete();
            for(i = 0; i < len; i++)
                if(fi[i].isDirectory())
                    result = deleteDirectory(fi[i].getPath());
                else
                    result = fi[i].delete();

            if(Dire.exists())
                result = Dire.delete();
        }
        catch(Exception e)
        {
            System.err.println(e.getMessage());
        }
        return result;
    }

    public boolean deleteRemoteDir(HttpServletRequest request, String filePath)
        throws Exception
    {
        boolean result = false;
        if(filePath != null)
        {
            String pathString = "";
            pathString = request.getRealPath("");
            pathString = pathString.replace('//', '/');
            pathString = pathString + filePath;
            int len = 0;
            int i = 0;
            try
            {
                File Dire = new File(pathString);
                if(!Dire.exists())
                    result = false;
                if(Dire.isFile())
                    result = Dire.delete();
                File fi[] = Dire.listFiles();
                len = fi.length;
                if(len == 0)
                    result = Dire.delete();
                for(i = 0; i < len; i++)
                    if(fi[i].isDirectory())
                        result = deleteDirectory(fi[i].getPath());
                    else
                        result = fi[i].delete();

                if(Dire.exists())
                    result = Dire.delete();
            }
            catch(Exception ex)
            {
                System.err.println(ex.getMessage());
            }
        }
        return result;
    }

    public boolean moveDirectory(String sourceDir, String desDir)
    {
        boolean result = false;
        int len = 0;
        int i = 0;
        sourceDir = sourceDir.replace('//', '/');
        desDir = desDir.replace('//', '/');
        String sourcePath = "";
        String desPath = "";
        String fileName = "";
        try
        {
            File Dire = new File(sourceDir);
            if(!Dire.exists())
                result = false;
            File fi[] = Dire.listFiles();
            len = fi.length;
            if(len == 0)
            {
                Dire.delete();
                result = true;
            }
            File d = new File(desDir);
            if(!d.exists())
                makeDir(desDir);
            for(i = 0; i < len; i++)
                if(fi[i].isDirectory())
                {
                    int last = fi[i].getPath().lastIndexOf("//");
                    String subdirname = fi[i].getPath().substring(last + 1, fi[i].getPath().length());
                    result = moveDirectory(fi[i].getPath(), desDir + "/" + subdirname);
                    if(result)
                        deleteDirectory(fi[i].getPath());
                } else
                {
                    fileName = fi[i].getName();
                    sourcePath = fi[i].getAbsolutePath();
                    desPath = desDir.replace('/', '//');
                    desPath = desPath + "//" + fileName;
                    result = movfile(sourcePath, desPath);
                }

        }
        catch(Exception e)
        {
            System.err.println(e.getMessage());
        }
        return result;
    }

    public boolean moveDirectory(String sourceDir, String desDir, String dirName)
    {
        boolean result = false;
        int len = 0;
        int i = 0;
        sourceDir = sourceDir.replace('//', '/');
        desDir = desDir.replace('//', '/');
        String sourcePath = "";
        String desPath = "";
        String fileName = "";
        try
        {
            File Dire = new File(sourceDir);
            if(!Dire.exists())
                result = false;
            File fi[] = Dire.listFiles();
            len = fi.length;
            if(len == 0)
                result = Dire.delete();
            File d = new File(desDir + "/" + dirName);
            if(!d.exists())
                result = makeDir(desDir + "/" + dirName);
            for(i = 0; i < len; i++)
                if(fi[i].isDirectory())
                {
                    int last = fi[i].getPath().lastIndexOf("//");
                    String subdirName = fi[i].getPath().substring(last + 1, fi[i].getPath().length());
                    result = moveDirectory(fi[i].getPath(), desDir + "/" + dirName + "/" + subdirName);
                    if(result)
                        deleteDirectory(fi[i].getPath());
                } else
                {
                    fileName = fi[i].getName();
                    sourcePath = fi[i].getAbsolutePath();
                    desPath = desDir.replace('/', '//');
                    desPath = desPath + "//" + dirName + "//" + fileName;
                    result = movfile(sourcePath, desPath);
                }

        }
        catch(Exception e)
        {
            System.err.println(e.getMessage());
        }
        return result;
    }

    public boolean createFile(String fullFileName, String txt)
    {
        boolean restult = false;
        try
        {
            if(txt == null)
                txt = "";
            int last = fullFileName.lastIndexOf("//");
            String dirName = fullFileName.substring(0, last);
            File Dire = new File(dirName);
            if(!Dire.exists())
                makeDir(dirName);
            PrintWriter pw = new PrintWriter(new FileOutputStream(fullFileName));
            pw.println(txt);
            restult = true;
            pw.close();
        }
        catch(Exception e)
        {
            System.err.println(e.getMessage());
        }
        return restult;
    }

    public boolean createRemoteFile(HttpServletRequest request, String fullFileName, String txt)
    {
        boolean restult = false;
        String pathString = "";
        pathString = request.getRealPath("");
        pathString = pathString.replace('//', '/');
        fullFileName = pathString + fullFileName;
        try
        {
            if(txt == null)
                txt = "";
            int last = fullFileName.lastIndexOf("/");
            String dirName = fullFileName.substring(0, last);
            dirName = dirName.replace('/', '//');
            File Dire = new File(dirName);
            if(!Dire.exists())
                makeDir(dirName);
            PrintWriter pw = new PrintWriter(new FileOutputStream(fullFileName));
            pw.println(txt);
            restult = true;
            pw.close();
        }
        catch(Exception e)
        {
            System.err.println(e.getMessage());
        }
        return restult;
    }

    public boolean deleteFile(String fullFileName)
    {
        boolean result = false;
        if(fullFileName == null || fullFileName == "")
            result = false;
        result = isExist(fullFileName);
        if(result)
        {
            fullFileName = StringUtils.replace(fullFileName, "//", "//");
            File fl = new File(fullFileName);
            result = fl.delete();
        }
        return result;
    }

    public boolean deleteRemoteFile(HttpServletRequest request, String filePath)
        throws Exception
    {
        boolean result = false;
        if(filePath != null)
        {
            String pathString = "";
            pathString = request.getRealPath("");
            pathString = pathString.replace('//', '/');
            pathString = pathString + filePath;
            try
            {
                File f = new File(pathString);
                if(f.exists())
                    result = f.delete();
            }
            catch(Exception ex) { }
        }
        return result;
    }

    public String getFileExtName(String fullFileName)
    {
        int i = 0;
        int Len = 0;
        String charStr = "";
        String rtn = "";
        if(fullFileName == null)
            return "";
        fullFileName = fullFileName.trim();
        Len = fullFileName.length();
        if(Len <= 1)
        {
            return "";
        } else
        {
            int last = fullFileName.lastIndexOf(".");
            rtn = fullFileName.substring(last + 1, Len);
            return rtn;
        }
    }

    public String getFileNoExtName(String fullFileName)
    {
        String rtn = "";
        String ext = "";
        if(fullFileName.length() <= 5)
        {
            return "";
        } else
        {
            fullFileName = fullFileName.replace('//', '/');
            ext = getFileExtName(fullFileName);
            int Start = fullFileName.lastIndexOf("/");
            rtn = fullFileName.substring(Start + 1, fullFileName.length() - ext.length() - 1);
            return rtn;
        }
    }

    public static final String getFile(String path)
    {
        String result = "";
        if(path.length() < 5)
            return "";
        try
        {
            path = path.trim();
            String str = "";
            int i = path.length();
            for(i = i; i > 0; i--)
            {
                str = path.substring(i - 1, i);
                if(str.equals("/") || str.equals("//"))
                    break;
                result = str + result;
            }

        }
        catch(Exception ex) { }
        return result;
    }

    public boolean isExist(String fullFileName)
    {
        if(fullFileName == null || fullFileName == "")
            return false;
        fullFileName = StringUtils.replace(fullFileName, "//", "//");
        File fl = new File(fullFileName);
        return fl.exists();
    }

    public boolean isRemoteExist(HttpServletRequest request, String fullFileName)
    {
        if(fullFileName == null || fullFileName == "")
            return false;
        String pathString = "";
        pathString = request.getRealPath("");
        pathString = pathString.replace('//', '/');
        pathString = pathString + fullFileName;
        File fl = new File(pathString);
        return fl.exists();
    }

    public boolean reName(String oldFileName, String newFileName)
    {
        boolean result = false;
        try
        {
            if(oldFileName == null || oldFileName == "" || newFileName == null || newFileName == "")
                result = false;
            else
            if(newFileName.indexOf("//") > 0 || newFileName.indexOf("/") > 0 || newFileName.indexOf(":") > 0 || newFileName.indexOf("*") > 0 || newFileName.indexOf("?") > 0 || newFileName.indexOf("|") > 0 || newFileName.indexOf("<") > 0 || newFileName.indexOf(">") > 0)
            {
                result = false;
            } else
            {
                oldFileName = StringUtils.replace(oldFileName, "//", "//");
                int last = oldFileName.lastIndexOf("//");
                String filePath = oldFileName.substring(0, last);
                File fl = new File(oldFileName);
                File f2 = new File(filePath + "//" + newFileName);
                result = fl.renameTo(f2);
            }
        }
        catch(Exception e)
        {
            result = false;
            System.err.println(e.getMessage());
        }
        return result;
    }

    public boolean reNameRemoteFile(HttpServletRequest request, String oldFileName, String newFileName)
    {
        boolean result = false;
        try
        {
            if(oldFileName == null || oldFileName == "" || newFileName == null || newFileName == "")
            {
                result = false;
            } else
            {
                String pathString = "";
                if(newFileName.indexOf("//") > -1 || newFileName.indexOf("/") > -1 || newFileName.indexOf(":") > -1 || newFileName.indexOf("*") > -1 || newFileName.indexOf("?") > -1 || newFileName.indexOf("|") > -1 || newFileName.indexOf("<") > 0 || newFileName.indexOf(">") > -1)
                {
                    result = false;
                } else
                {
                    pathString = request.getRealPath("");
                    pathString = pathString.replace('//', '/');
                    pathString = pathString + oldFileName;
                    int last = pathString.lastIndexOf("/");
                    String filePath = pathString.substring(0, last);
                    File fl = new File(pathString);
                    File f2 = new File(filePath + "/" + newFileName);
                    result = fl.renameTo(f2);
                }
            }
        }
        catch(Exception e)
        {
            result = false;
            System.err.println(e.getMessage());
        }
        return result;
    }

    public boolean moveRemoteFile(HttpServletRequest request, String src, String des)
        throws Exception
    {
        boolean result = false;
        try
        {
            String pathString = request.getRealPath("");
            pathString = pathString.replace('//', '/');
            des = pathString + des;
            src = pathString + src;
            if(!isExist(des))
                result = makeDir(des);
            int len = src.length();
            int last = src.lastIndexOf("/");
            String filename = src.substring(last + 1, len);
            des = des + filename;
            FileInputStream fi = new FileInputStream(src);
            BufferedInputStream ipt = new BufferedInputStream(fi);
            FileOutputStream fo = new FileOutputStream(des);
            BufferedOutputStream opt = new BufferedOutputStream(fo);
            for(boolean eof = false; !eof;)
            {
                int input = ipt.read();
                if(input == -1)
                    break;
                opt.write(input);
            }

            ipt.close();
            opt.close();
            File Source = new File(src);
            if(Source.delete())
                result = true;
        }
        catch(Exception e)
        {
            result = false;
            System.err.println(e.getMessage());
        }
        return result;
    }

    public boolean moveFile(String src, String des)
        throws Exception
    {
        boolean result = false;
        try
        {
            if(!isExist(des))
                result = makeDir(des);
            int len = src.length();
            int last = src.lastIndexOf("//");
            String filename = src.substring(last + 1, len);
            des = des + filename;
            FileInputStream fi = new FileInputStream(src);
            BufferedInputStream ipt = new BufferedInputStream(fi);
            FileOutputStream fo = new FileOutputStream(des);
            BufferedOutputStream opt = new BufferedOutputStream(fo);
            for(boolean eof = false; !eof;)
            {
                int input = ipt.read();
                if(input == -1)
                    break;
                opt.write(input);
            }

            ipt.close();
            opt.close();
            File Source = new File(src);
            if(Source.delete())
                result = true;
        }
        catch(Exception e)
        {
            result = false;
            System.err.println(e.getMessage());
        }
        return result;
    }

    private boolean movfile(String src, String des)
        throws Exception
    {
        boolean result = false;
        try
        {
            FileInputStream fi = new FileInputStream(src);
            BufferedInputStream ipt = new BufferedInputStream(fi);
            FileOutputStream fo = new FileOutputStream(des);
            BufferedOutputStream opt = new BufferedOutputStream(fo);
            for(boolean eof = false; !eof;)
            {
                int input = ipt.read();
                if(input == -1)
                    break;
                opt.write(input);
            }

            ipt.close();
            opt.close();
            File Source = new File(src);
            if(Source.delete())
                result = true;
        }
        catch(Exception e)
        {
            result = false;
            System.err.println(e.getMessage());
        }
        return result;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值