巷尾的酒吧

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            64 Posts :: 0 Stories :: 5 Comments :: 0 Trackbacks
          package com.abin.test.result;

          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import java.io.IOException;
          //以前做Java很少會用到文件,原因之一是我做的項目的那一塊很少用到文件操作,只用過文件上傳,應我同學的要求,今天幫他實現了一下,放在這里

          //以備以后再用的著的話好找,這些都是參考網上好多人的心血而成的。
          public class CopyFile1 {
          //復制文件并且改名:
              
          public static void doCopyFile(String fromPath ,String toPath) throws IOException{
                  String toNewPath
          =toPath+fromPath.subSequence(fromPath.lastIndexOf("\\")+1, fromPath.length());
                  File srcFile 
          = new File(fromPath);
                  File destFile 
          = new File(toNewPath);
                  FileInputStream input 
          = new FileInputStream(srcFile);
                  
          try {
                      FileOutputStream output 
          = new FileOutputStream(destFile);
                      
          try {
                          
          byte[] buffer = new byte[4096];
                          
          int n = 0;
                          
          while (-1 != (n = input.read(buffer))) {
                              output.write(buffer, 
          0, n);
                          }
                      } 
          finally {
                          
          try {
                              
          if (output != null) {
                                  output.close();
                              }
                          } 
          catch (IOException ioe) {
                              
          // ignore
                          }
                      }
                  } 
          finally {
                      
          try {
                          
          if (input != null) {
                              input.close();
                          }
                      } 
          catch (IOException ioe) {
                          
          // ignore
                      }
                  }
              }
          //移動文件并且改名:
              
          public static void doCopyFileAndUpdateFileName(String fromPath ,String toPath,String newName) throws 

          IOException{
                  String toNewPath
          =toPath+newName;
                  File srcFile 
          = new File(fromPath);
                  File destFile 
          = new File(toNewPath);
                  FileInputStream input 
          = new FileInputStream(srcFile);
                  
          try {
                      FileOutputStream output 
          = new FileOutputStream(destFile);
                      
          try {
                          
          byte[] buffer = new byte[4096];
                          
          int n = 0;
                          
          while (-1 != (n = input.read(buffer))) {
                              output.write(buffer, 
          0, n);
                          }
                      } 
          finally {
                          
          try {
                              
          if (output != null) {
                                  output.close();
                              }
                          } 
          catch (IOException ioe) {
                              
          // ignore
                          }
                      }
                  } 
          finally {
                      
          try {
                          
          if (input != null) {
                              input.close();
                          }
                      } 
          catch (IOException ioe) {
                          
          // ignore
                      }
                  }
              }
          //刪除文件:
              
          public static void doDeleteFile(String path) throws IOException{
                  File file
          =new File(path);
                  
          if(file.exists()||file.isFile()||!file.isDirectory()){
                      file.delete();
                      file.deleteOnExit();
                  }
              }
          //修改文件名:
              
          public static void doUpdateFileName(String path1,String newName1) throws IOException{
                  String path
          =path1.substring(0, path1.lastIndexOf("\\"))+"\\";
                  System.out.println(path);
                  File newFile
          =new File(path+newName1);
                  File file
          =new File(path1);
                  file.renameTo(newFile);
                     
              }
              
          public static void main(String args[])throws Exception {
                  String fromPath
          ="e:\\love.jpg";
                  String toPath
          ="e:\\demo\\";
                  String newName
          ="abin.jpg";
                  String newName1
          ="abin.jpg";
                  String path
          ="e:\\demo\\love.jpg";
                  String path1
          ="e:\\demo\\love.jpg";
              
          //    new CopyFile1().doCopyFile(fromPath, toPath);
              
          //    new CopyFile1().doCopyFileAndUpdateFileName(fromPath, toPath, newName);
              
          //    new CopyFile1().doDeleteFile(path);
                  new CopyFile1().doUpdateFileName(path1, newName1);
                  
                  CopyFile1 aaa
          =new CopyFile1();
                  aaa.doCopyFile(fromPath, toPath);
                  
                  
              }

          }
          posted on 2011-09-28 23:24 abing 閱讀(242) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 新沂市| 新河县| 启东市| 龙泉市| 大悟县| 乌鲁木齐县| 建平县| 勐海县| 正宁县| 综艺| 三门县| 上虞市| 锦州市| 林西县| 大同市| 新晃| 广东省| 揭西县| 通许县| 林甸县| 察隅县| 桑植县| 辽宁省| 彝良县| 南丰县| 陇西县| 永顺县| 循化| 新昌县| 绿春县| 宁都县| 黄大仙区| 桂阳县| 华坪县| 元江| 原平市| 肇庆市| 合川市| 万盛区| 姚安县| 比如县|