I/O的基本操作

java文件复制  
  1.   
  2.   
  3.    
  4. ---------------------------------------------------------------    
  5.    
  6. java.io    
  7. ---------------------------------------------------------------    
  8.    
  9.     
  10.    
  11. package  common;    
  12.    
  13. import  java.io.*;    
  14.    
  15. public  class  FileOperate  {    
  16.    public  FileOperate()  {    
  17.    }    
  18.    
  19.        
  20.    public  void  newFolder(String  folderPath)  {    
  21.        try  {    
  22.            String  filePath  =  folderPath;    
  23.            filePath  =  filePath.toString();    
  24.            java.io.File  myFilePath  =  new  java.io.File(filePath);    
  25.            if  (!myFilePath.exists())  {    
  26.                myFilePath.mkdir();    
  27.            }    
  28.        }    
  29.        catch  (Exception  e)  {    
  30.            System.out.println("新建目录操作出错");    
  31.            e.printStackTrace();    
  32.        }    
  33.    }    
  34.    
  35.        
  36.    public  void  newFile(String  filePathAndName,  String  fileContent)  {    
  37.    
  38.        try  {    
  39.            String  filePath  =  filePathAndName;    
  40.            filePath  =  filePath.toString();    
  41.            File  myFilePath  =  new  File(filePath);    
  42.            if  (!myFilePath.exists())  {    
  43.                myFilePath.createNewFile();    
  44.            }    
  45.            FileWriter  resultFile  =  new  FileWriter(myFilePath);    
  46.            PrintWriter  myFile  =  new  PrintWriter(resultFile);    
  47.            String  strContent  =  fileContent;    
  48.            myFile.println(strContent);    
  49.            resultFile.close();    
  50.    
  51.        }    
  52.        catch  (Exception  e)  {    
  53.            System.out.println("新建目录操作出错");    
  54.            e.printStackTrace();    
  55.    
  56.        }    
  57.    
  58.    }    
  59.    
  60.        
  61.    public  void  delFile(String  filePathAndName)  {    
  62.        try  {    
  63.            String  filePath  =  filePathAndName;    
  64.            filePath  =  filePath.toString();    
  65.            java.io.File  myDelFile  =  new  java.io.File(filePath);    
  66.            myDelFile.delete();    
  67.    
  68.        }    
  69.        catch  (Exception  e)  {    
  70.            System.out.println("删除文件操作出错");    
  71.            e.printStackTrace();    
  72.    
  73.        }    
  74.    
  75.    }    
  76.    
  77.        
  78.    public  void  delFolder(String  folderPath)  {    
  79.        try  {    
  80.            delAllFile(folderPath);  //删除完里面所有内容    
  81.            String  filePath  =  folderPath;    
  82.            filePath  =  filePath.toString();    
  83.            java.io.File  myFilePath  =  new  java.io.File(filePath);    
  84.            myFilePath.delete();  //删除空文件夹    
  85.    
  86.        }    
  87.        catch  (Exception  e)  {    
  88.            System.out.println("删除文件夹操作出错");    
  89.            e.printStackTrace();    
  90.    
  91.        }    
  92.    
  93.    }    
  94.    
  95.        
  96.    public  void  delAllFile(String  path)  {    
  97.        File  file  =  new  File(path);    
  98.        if  (!file.exists())  {    
  99.            return;    
  100.        }    
  101.        if  (!file.isDirectory())  {    
  102.            return;    
  103.        }    
  104.        String[]  tempList  =  file.list();    
  105.        File  temp  =  null;    
  106.        for  (int  i  =  0;  i  <  tempList.length;  i++)  {    
  107.            if  (path.endsWith(File.separator))  {    
  108.                temp  =  new  File(path  +  tempList[i]);    
  109.            }    
  110.            else  {    
  111.                temp  =  new  File(path  +  File.separator  +  tempList[i]);    
  112.            }    
  113.            if  (temp.isFile())  {    
  114.                temp.delete();    
  115.            }    
  116.            if  (temp.isDirectory())  {    
  117.                delAllFile(path+"/"+  tempList[i]);//先删除文件夹里面的文件    
  118.                delFolder(path+"/"+  tempList[i]);//再删除空文件夹    
  119.            }    
  120.        }    
  121.    }    
  122.    
  123.        
  124.    public  void  copyFile(String  oldPath,  String  newPath)  {    
  125.        try  {    
  126.            int  bytesum  =  0;    
  127.            int  byteread  =  0;    
  128.            File  oldfile  =  new  File(oldPath);    
  129.            if  (oldfile.exists())  {  //文件存在时    
  130.                InputStream  inStream  =  new  FileInputStream(oldPath);  //读入原文件    
  131.                FileOutputStream  fs  =  new  FileOutputStream(newPath);    
  132.                byte[]  buffer  =  new  byte[1444];    
  133.                int  length;    
  134.                while  (  (byteread  =  inStream.read(buffer))  !=  -1)  {    
  135.                    bytesum  +=  byteread;  //字节数  文件大小    
  136.                    System.out.println(bytesum);    
  137.                    fs.write(buffer,  0,  byteread);    
  138.                }    
  139.                inStream.close();    
  140.            }    
  141.        }    
  142.        catch  (Exception  e)  {    
  143.            System.out.println("复制单个文件操作出错");    
  144.            e.printStackTrace();    
  145.    
  146.        }    
  147.    
  148.    }    
  149.    
  150.        
  151.    public  void  copyFolder(String  oldPath,  String  newPath)  {    
  152.    
  153.        try  {    
  154.            (new  File(newPath)).mkdirs();  //如果文件夹不存在  则建立新文件夹    
  155.            File  a=new  File(oldPath);    
  156.            String[]  file=a.list();    
  157.            File  temp=null;    
  158.            for  (int  i  =  0;  i  <  file.length;  i++)  {    
  159.                if(oldPath.endsWith(File.separator)){    
  160.                    temp=new  File(oldPath+file[i]);    
  161.                }    
  162.                else{    
  163.                    temp=new  File(oldPath+File.separator+file[i]);    
  164.                }    
  165.    
  166.                if(temp.isFile()){    
  167.                    FileInputStream  input  =  new  FileInputStream(temp);    
  168.                    FileOutputStream  output  =  new  FileOutputStream(newPath  +  "/"  +    
  169.                            (temp.getName()).toString());    
  170.                    byte[]  b  =  new  byte[1024  *  5];    
  171.                    int  len;    
  172.                    while  (  (len  =  input.read(b))  !=  -1)  {    
  173.                        output.write(b,  0,  len);    
  174.                    }    
  175.                    output.flush();    
  176.                    output.close();    
  177.                    input.close();    
  178.                }    
  179.                if(temp.isDirectory()){//如果是子文件夹    
  180.                    copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);    
  181.                }    
  182.            }    
  183.        }    
  184.        catch  (Exception  e)  {    
  185.            System.out.println("复制整个文件夹内容操作出错");    
  186.            e.printStackTrace();    
  187.    
  188.        }    
  189.    
  190.    }    
  191.    
  192.        
  193.    public  void  moveFile(String  oldPath,  String  newPath)  {    
  194.        copyFile(oldPath,  newPath);    
  195.        delFile(oldPath);    
  196.    
  197.    }    
  198.    
  199.        
  200.    public  void  moveFolder(String  oldPath,  String  newPath)  {    
  201.        copyFolder(oldPath,  newPath);    
  202.        delFolder(oldPath);    
  203.    
  204.    }    
  205. }   
posted @ 2015-05-13 14:24  七夏之  阅读(127)  评论(0编辑  收藏  举报