java 文件操作

public class DocumentUtil{
String startFilePath = null;  
String desFilePath = null;  
// 复制文件(夹)  
public boolean copy(String startFilePath, String desFilePath) {  
  
        this.startFilePath = startFilePath;  
        this.desFilePath = desFilePath;  
  
        // 判断是否返回成功的变量  
        boolean copyFinished = false;  
  
        File startFile = new File(startFilePath);  
        File desFile = new File(desFilePath);  
  
        // 如果源文件是个文件  
        if (startFile.isFile()) {  
  
            copyFinished = this.copySingleFile(startFile, desFile);  
  
            // 如果源文件是个文件夹,就需要递归复制  
        } else {  
  
            //如果目标文件夹是源文件夹的一个子目录的情况,拒绝复制,因为会造成无限循环  
            if(desFilePath.startsWith(startFilePath+"/")){  
                System.out.println("error copy");  
                return false;  
                  
            }else  
              
            copyFinished = this.copyFolder(startFile, desFile);  
          
        }  
  
      
        return copyFinished;  
    }  

// 显示目录下所有文件ming  
    public static File[] getDocuments(String path)  
    {  
        File file = new File(path);  
        File[] files = file.listFiles();  
        return files;     
    }  
    // 新建文件夹  
    public static boolean newFile(String path)  
    {  
        File   file   =   new   File(path);             
        if(file.exists()){     
            System.out.println("the   file   is   exits");  
            return false;  
        }     
        else{     
        file.mkdir();     
        System.out.println("new ci file success!"   );   
        return true;  
        }     
    }

/** 
     * 此方法为复制单个文件,如果复制多个文件可以递归调用 
     */  
    private boolean copySingleFile(File startSingleFile, File desSingleFile) {  
  
        boolean rightCopy = false;  
  
        // -------从源文件中输入内存入byte中,在将byte写入目标文件--------------------  
        FileInputStream singleFileInputStream = null;  
        DataInputStream singleDataInputStream = null;  
        FileOutputStream singleFileOutputStream = null;  
        DataOutputStream singleDataOutputStream = null;  
          
        try {  
  
            singleFileInputStream = new FileInputStream(startSingleFile);  
  
            singleDataInputStream = new DataInputStream(  
                    new BufferedInputStream(singleFileInputStream));  
  
            singleFileOutputStream = new FileOutputStream(desSingleFile);  
  
            singleDataOutputStream = new DataOutputStream(  
                    new BufferedOutputStream(singleFileOutputStream));  
  
            byte[] b = new byte[1024];  
  
            int len;  
            while ((len = singleDataInputStream.read(b)) != -1) {  
  
                singleDataOutputStream.write(b, 0, len);  
  
            }  
            //刷新缓冲区  
            singleDataOutputStream.flush();  
  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            try {  
  
                if (singleDataInputStream != null)  
                    singleDataInputStream.close();  
                if (singleDataOutputStream != null)  
                    singleDataOutputStream.close();  
  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
          
        //判断源文件和目标文件大小是否相同,如果相同证明复制成功  
        if (startSingleFile.length() == desSingleFile.length())  
            rightCopy = true;  
        else  
            rightCopy = false;  
  
        return rightCopy;  
  
    }  

public static void format(OutputStream os,Document doc)throws Exception{  
          
        String sysEncoding = System.getProperty("sun.jnu.encoding");  
        Serializer serializer = new Serializer(os,sysEncoding);  
        serializer.setIndent(4);//怎么缩进  
        serializer.setMaxLength(600);  
        serializer.write(doc);//写出  
        serializer.flush();//清空缓存  
    }  
public boolean copyFolder(File startFolder, File desFolder) {  
  
  
        boolean rightCopy = false;  
  
  
        rightCopy = this.recursionCopy(startFolder, desFolder);  
  
  
        return rightCopy;  
    }  

/** 
     * 复制文件夹函数,此函数是个递归,会复制文件夹下的所有文件 
     *  
     * @param recFileFolder 
     *            = 需要拷贝的文件夹或子文件夹 
     * @param recDesFolder 
     *            = 拷贝的目的文件夹或子文件夹, 
     * @return = true表示成功, false表示失败 
     */  
    private boolean recursionCopy(File recFileFolder, File recDesFolder) {  
   
        File desFolder = recDesFolder;  
  
        //因为目的文件夹或子文件夹不存在,需要创建  
        desFolder.mkdir();  
          
        //此为需要拷贝的文件夹下的所有文件列表(其中有文件和文件夹)  
        File[] files = recFileFolder.listFiles();  
          
        //如果是个空文件夹  
        if(files.length==0) return true;  
          
      
        /* 
         * 将文件夹下所有文件放入for循环,如果是文件,那么调用copySingleFile()拷贝文件, 
         * 如果是文件夹,那么递归调用此函数 
         *   
         */  
        for (File thisFile : files) {  
              
            // 如果此文件是个文件,那么直接调用单个文件复制命令复制文件  
            if (thisFile.isFile()) {  
                // 得到此文件的新位置地址  
                String desContentFilePath = this.getSubFilePath(startFilePath,desFilePath, thisFile.getAbsolutePath());  
  
                boolean rightCopy = this.copySingleFile(thisFile, new File(desContentFilePath));  
  
                // 如果复制失败,就跳出循环停止复制  
                if(!rightCopy) return false;  
  
                // 如果是个文件夹  
            } else {  
  
                String desContentFilePath = this.getSubFilePath(startFilePath,desFilePath, thisFile.getAbsolutePath());  
                // 递归的调用此函数,确保函数都被复制完全  
                boolean rightCopy = recursionCopy(thisFile, new File(desContentFilePath));  
                        if(!rightCopy) return false;  
            }  
  
        }  
        return true;  
    }  
/** 
     * 此函数是为了得到目的文件夹的地址, 
     * 如:源文件夹为:D:/addMe/text (其中text文件夹下有另一个文件夹 second :  D:/addMe/text/second)   
     *          目标位置为:E:/aa/text 
     *    那么此second文件夹在目标地址的位置就是 E:/aa/text/second 
     *    此方法中 startFolderPath = D:/addMe/text (源文件夹) ;   
     *           desFolderPath = E:/addMe/text (目标位置);  
     *           currentFilePath = D:/addMe/text/second(需要复制的子文件夹) 
     *         返回值为: E:/addMe/text/second 
     */  
    private String getSubFilePath(String startFolderPath, String desFolderPath,  
            String currentFilePath) {  
  
        String currentDesFilePath = null;  
  
        int i = startFolderPath.length();  
          
        //int j = desFolderPath.lastIndexOf("/");  
  
        //String subDirPath = startFolderPath.substring(0, i);  
        //String subDesPath = desFolderPath.substring(0, j);  
  
        currentDesFilePath = desFolderPath + "/"  
                + currentFilePath.substring(i+1);  
  
        return currentDesFilePath;  
  
    }  

/** 
     *  
     * @param path 文件路径 
     * @param comment 文件中用于注释的符号 
     * @return 
     */  
    public static String readFile(String path,String comment)  
    {  
        String str = "";  
        File file = new File(path);  
        BufferedReader reader = null;  
        try {  
         reader = new BufferedReader(new FileReader(file));  
         String tempString = null;  
         String br= System.getProperty("line.separator");   
         // 一次读入一行,直到读入null为文件结束  
         while ((tempString = reader.readLine()) != null) {  
          // 去除注释  
          if(!StringUtil.isEmpty(comment)&&tempString.trim().startsWith(comment))  
          {  
              continue;  
          }  
          str = str+ tempString+br;  
         }  
         reader.close();  
        } catch (IOException e) {  
         e.printStackTrace();  
        } finally {  
         if (reader != null) {  
          try {  
           reader.close();  
          } catch (IOException e1) {  
          }  
         }  
        }  
             
         return str;  
    }  
      
    public static void writeToFile(String filePath, String sets) throws IOException {  
        FileWriter fw = new FileWriter(filePath);  
        PrintWriter out = new PrintWriter(fw);  
        out.write(sets);  
        out.println();  
        fw.close();  
        out.close();  
       }  

public static void renameFile(String path,String newName) throws IOException  
    {  
          File oldFile = new File(path);  
          if(!oldFile.exists())  
          {  
           //oldFile.createNewFile();  
           System.out.println(path+" 不存在,无法重命名!");  
          }  
          String rootPath = oldFile.getParent();  
          File newFile = new File(rootPath + File.separator + newName);  
          if (oldFile.renameTo(newFile))   
          {  
           System.out.println("重命名成功!");  
          }   
          else   
          {  
           System.out.println("重命名失败!");  
          }  
    }  
      
    public static boolean isDocumentExsist(String path,String fileName)  
    {  
        File[] files= DocumentUtil.getDocuments(path);  
        boolean isExsist = false;  
        for(int i=0;i<files.length;i++)  
        {  
            if(files[i].getName().equals(fileName))  
             {  
                isExsist =  true;  
                break;  
             }  
        }  
        return isExsist;  
    }  
      
    public static void deleteCommonFiles(File[] file1,File[] file2)  
    {  
        //List list = new ArrayList<String>();  
        for(int i=0;i<file1.length;i++)  
        {  
            String name1 = file1[i].getName();  
            if(name1.endsWith(".jar"))  
            {  
                for(int j=0;j<file2.length;j++)  
                {  
                    String name2 = file2[j].getName();  
                    if(name1.equals(name2))  
                    {  
                        //shanchu  
                        System.out.println("删除重复的jar:"+name1);  
                        file1[i].delete();  
                    }  
                }  
            }  
        }  
        //return list;  
    }  
      
    /** 
     * 判断路径是否存在 
     * @param path 
     * @return 
     */  
    public static boolean isPathExsist(String path)  
    {  
        File file = new File(path);  
        return file.exists();  
    }  

  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值