loveofgod 发表于 2013-1-27 05:37:42

java文件操作类

package com.roam.base;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;

import com.baseclass.CString;

public class FileOperate
{
/**
       * 新建文件操作
       * 方法名:NewsFile
       * 参       数:
       *      String       FilePath //要创建文件的路径(已有的文件路径)
       *      String       Content       //要写入文件的内容
       *      boolean Bestrow       //如果有这个文件,是否覆盖(true,false)
       * 返回值:boolean类型
       * 功       能:在用户指定的路径下,进行新建文件操作
       * 备       注:此方法只针对文件进行操作而不对目录操作
       */
public boolean NewFile(String FilePath,String Content,boolean Bestrow)
{
       File file = new File(FilePath);
       //如果不覆盖已有的文件直接返回假
       if(!Bestrow && file.exists())
       {
      return false;
       }
       try
       {
      //判断文件或者目录是否存在
      if(!file.exists())
      {
         file.createNewFile();
      }
      FileWriter fileWriter = new FileWriter(file);
      PrintWriter printWriter = new PrintWriter(fileWriter);
      printWriter.println(Content);
      fileWriter.close();
      return true;
       }catch(Exception e)
       {
      System.out.println("FileOperate::NewFile"+e.getMessage());
      return false;
       }
}
/**
       * 新建目录操作
       * 方法名:NewsFolder
       * 参       数:
       *      String       FolderPath //要创建目录的路径
       *      boolean Bestrow         //如果有这个目录,是否覆盖(ture,false)
       * 返回值:boolean类型
       * 功       能:在用户指定的路径下,进行新建目录操作
       * 备       注:该功能只能在指定的路径下,建立一级目录
       */
public boolean NewFolder(String FolderPath,boolean Bestrow)
{
       File file = new File(FolderPath);
       //如果不覆盖已有的目录直接返回假
       if(!Bestrow && file.exists())
       {
      return false;
       }
       try
       {
      if(!file.exists())
      {
         file.mkdir();
      }
      return true;
       }catch(Exception e)
       {
      System.out.println("FileOperate::NewFolder"+e.getMessage());
      return false;
       }
}
/**
       * 删除文件操作
       * 方法名:DeleteFile
       * 参       数:
       *      String       FilePath //要删除文件的路径
       * 返回值:boolean类型
       * 功       能:在用户指定的路径下,进行删除文件操作
       * 备       注:该操作只针对指定目录下的文件进行删除操作,和空文件夹
       */
public boolean DeleteFile(String FilePath)
{
       File file = new File(FilePath);
       try
       {
      if(file.exists())
      {
         file.delete();
      }
      return true;
       }catch(Exception e)
       {
      System.out.println("FileOperate::DeleteFile"+e.getMessage());
      return false;
       }
}
/**
       * 删除文件目录下的所有文件操作
       * 方法名:DeleteFolder
       * 参       数:
       *      String       FolderPath //要删除目录的路径
       * 返回值:boolean类型
       * 功       能:根据用户指定的目录,进行删除该目录下的所有文件操作
       * 备       注:
       */
public boolean DeleteFolder(String FolderPath)
{
       File file=new File(FolderPath);
       //检查参数
       if(!file.exists())
       {
      return false;
       }
       if(!file.isDirectory())
       {
      return false;
       }
       String[] tempList=file.list();
       File temp=null;
       for(int i=0;i<tempList.length;i++)
       {
      if(FolderPath.endsWith(File.separator))
      {
         temp=new File(FolderPath+tempList);
      }
      else
      {
         temp=new File(FolderPath+File.separator+tempList);
      }
      if(temp.isFile())
      {
         temp.delete();
      }
      if(temp.isDirectory())
      {
         DeleteFolder(FolderPath+"/"+tempList);
         DeleteFile(FolderPath+"/"+tempList);
      }
       }
       return false;
}
/**
       * 删除目录操作
       * 方法名:DeleteAll
       * 参       数:
       *      String       FolderPath //要删除目录的路径
       * 返回值:boolean类型
       * 功       能:根据用户指定的目录,进行删除该目录和目录下所有的文件操作
       * 备       注:递归方法
       */
public boolean DeleteAll(String FolderPath)
{
       try
       {
      File file=new File(FolderPath);
      if(!file.isDirectory())
      {
         return false;
      }
      File[] fileList=file.listFiles();
      for(int i=0;i<fileList.length;i++)
      {
         if(fileList.isDirectory())
         {
          if(!this.DeleteAll(fileList.toString()))
          {
         return false;
          }
         }
         else
         {
          if(!fileList.delete())
          {
         return false;
          }
         }
      }
      if(!file.delete())
      {
         return false;
      }
      else
      {
         return true;
      }
       }catch(Exception e)
       {
      System.out.println("FileOperate::DeleteAll"+e.getMessage());
      return false;
       }
}
/**
       * 创建多级目录操作
       * 方法名:CreateFolder
       * 参       数:
       *      String       FolderPath //要创建的目录
       * 返回值:boolean类型
       * 功       能:根据用户指定的多级目录进行创建
       * 备       注:
       */
public boolean CreateFolder(String FolderPath)
{
       File file1=new File(FolderPath);
       //检查参数
       if(FolderPath==null || FolderPath.length()==0)
       {
      return false;
       }
       if(FolderPath.indexOf("/")==-1 && FolderPath.indexOf("\\")==-1)
       {
      return false;
       }
       if(file1.exists())
       {
      return true;
       }
       //首次处理传过来的字符串
       String str_temp="";
       for(int i=0;i<FolderPath.length();i++)
       {
      if(i<FolderPath.length())
      {
         if(FolderPath.substring(i,i+1).equals("\\"))
         {
          str_temp+="/";
         }
         else
         {
          str_temp+=FolderPath.substring(i,i+1);
         }
      }
       }
       //通过"/",那字符串拆分
       String Str_P[]=CString.Split(str_temp,"/");
       String Str_Create="";
       for(int i=0;i<Str_P.length;i++)
       {
      Str_Create+=Str_P+"/";
      File file=new File(Str_Create);
      if(!file.exists())
      {
         if(!file.mkdir())
         {
          return false;
         }
      }
       }
       return true;
}
/**
       * 主方法用于测试操作
       */
public static void main(String[] args)
{
       FileOperate fileOperate = new FileOperate();
       //fileOperate.NewFolder("c://gameboy",false);
       //fileOperate.NewFile("c://text.txt","中文",true);
       //fileOperate.DeleteFile("c://a");
       //fileOperate.CreateFolder("c:\\a\\b\\c\\d\\e");
       //fileOperate.DeleteFolder("c://a");
       //fileOperate.DeleteAll("c://a");
}
}
页: [1]
查看完整版本: java文件操作类