巷尾的酒吧

            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)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 瑞昌市| 襄城县| 陈巴尔虎旗| 六安市| 贞丰县| 南溪县| 弥渡县| 柘荣县| 靖州| 民和| 石柱| 温泉县| 常熟市| 屏东县| 安图县| 尼勒克县| 绿春县| 老河口市| 万山特区| 商南县| 武穴市| 贵溪市| 铜川市| 江西省| 讷河市| 塔城市| 盐城市| 嵩明县| 乾安县| 兴国县| 福贡县| 河西区| 柳河县| 遂溪县| 荣昌县| 绿春县| 永德县| 平武县| 汉中市| 耿马| 晋城|