最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實(shí)現(xiàn)文件復(fù)制上傳操作

 更新時間:2016年11月30日 14:06:41   作者:qq_27298687  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)文件復(fù)制上傳操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下

利用Java復(fù)制文件到處都可以用到,這里總結(jié)了一個類供大家參考。里面總共有兩個方法:

public static boolean copyFile(String srcFileName, String destFileName,boolean overlay); 
public static boolean copyDirectory(String srcDirName, String destDirName,boolean overlay) ; 

其中:
srcFileName 待復(fù)制的文件名
descFileName  目標(biāo)文件名
overlay  如果目標(biāo)文件存在,是否覆蓋
如果復(fù)制成功返回true,否則返回false

代碼:

 import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
 
import javax.swing.JOptionPane; 
 
/** 
 * 復(fù)制文件或文件夾 
 * 
 * zww 
 */ 
public class CopyFileUtil { 
 
  private static String MESSAGE = ""; 
 
  /** 
   * 復(fù)制單個文件 
   * 
   * @param srcFileName 
   *      待復(fù)制的文件名 
   * @param descFileName 
   *      目標(biāo)文件名 
   * @param overlay 
   *      如果目標(biāo)文件存在,是否覆蓋 
   * @return 如果復(fù)制成功返回true,否則返回false 
   */ 
  public static boolean copyFile(String srcFileName, String destFileName, 
      boolean overlay) { 
    File srcFile = new File(srcFileName); 
 
    // 判斷源文件是否存在 
    if (!srcFile.exists()) { 
      MESSAGE = "源文件:" + srcFileName + "不存在!"; 
      JOptionPane.showMessageDialog(null, MESSAGE); 
      return false; 
    } else if (!srcFile.isFile()) { 
      MESSAGE = "復(fù)制文件失敗,源文件:" + srcFileName + "不是一個文件!"; 
      JOptionPane.showMessageDialog(null, MESSAGE); 
      return false; 
    } 
 
    // 判斷目標(biāo)文件是否存在 
    File destFile = new File(destFileName); 
    if (destFile.exists()) { 
      // 如果目標(biāo)文件存在并允許覆蓋 
      if (overlay) { 
        // 刪除已經(jīng)存在的目標(biāo)文件,無論目標(biāo)文件是目錄還是單個文件 
        new File(destFileName).delete(); 
      } 
    } else { 
      // 如果目標(biāo)文件所在目錄不存在,則創(chuàng)建目錄 
      if (!destFile.getParentFile().exists()) { 
        // 目標(biāo)文件所在目錄不存在 
        if (!destFile.getParentFile().mkdirs()) { 
          // 復(fù)制文件失敗:創(chuàng)建目標(biāo)文件所在目錄失敗 
          return false; 
        } 
      } 
    } 
 
    // 復(fù)制文件 
    int byteread = 0; // 讀取的字節(jié)數(shù) 
    InputStream in = null; 
    OutputStream out = null; 
 
    try { 
      in = new FileInputStream(srcFile); 
      out = new FileOutputStream(destFile); 
      byte[] buffer = new byte[1024]; 
 
      while ((byteread = in.read(buffer)) != -1) { 
        out.write(buffer, 0, byteread); 
      } 
      return true; 
    } catch (FileNotFoundException e) { 
      return false; 
    } catch (IOException e) { 
      return false; 
    } finally { 
      try { 
        if (out != null) 
          out.close(); 
        if (in != null) 
          in.close(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
      } 
    } 
  } 
 
  /** 
   * 復(fù)制整個目錄的內(nèi)容 
   * 
   * @param srcDirName 
   *      待復(fù)制目錄的目錄名 
   * @param destDirName 
   *      目標(biāo)目錄名 
   * @param overlay 
   *      如果目標(biāo)目錄存在,是否覆蓋 
   * @return 如果復(fù)制成功返回true,否則返回false 
   */ 
  public static boolean copyDirectory(String srcDirName, String destDirName, 
      boolean overlay) { 
    // 判斷源目錄是否存在 
    File srcDir = new File(srcDirName); 
    if (!srcDir.exists()) { 
      MESSAGE = "復(fù)制目錄失?。涸茨夸? + srcDirName + "不存在!"; 
      JOptionPane.showMessageDialog(null, MESSAGE); 
      return false; 
    } else if (!srcDir.isDirectory()) { 
      MESSAGE = "復(fù)制目錄失敗:" + srcDirName + "不是目錄!"; 
      JOptionPane.showMessageDialog(null, MESSAGE); 
      return false; 
    } 
 
    // 如果目標(biāo)目錄名不是以文件分隔符結(jié)尾,則加上文件分隔符 
    if (!destDirName.endsWith(File.separator)) { 
      destDirName = destDirName + File.separator; 
    } 
    File destDir = new File(destDirName); 
    // 如果目標(biāo)文件夾存在 
    if (destDir.exists()) { 
      // 如果允許覆蓋則刪除已存在的目標(biāo)目錄 
      if (overlay) { 
        new File(destDirName).delete(); 
      } else { 
        MESSAGE = "復(fù)制目錄失?。耗康哪夸? + destDirName + "已存在!"; 
        JOptionPane.showMessageDialog(null, MESSAGE); 
        return false; 
      } 
    } else { 
      // 創(chuàng)建目的目錄 
      System.out.println("目的目錄不存在,準(zhǔn)備創(chuàng)建。。。"); 
      if (!destDir.mkdirs()) { 
        System.out.println("復(fù)制目錄失?。簞?chuàng)建目的目錄失敗!"); 
        return false; 
      } 
    } 
 
    boolean flag = true; 
    File[] files = srcDir.listFiles(); 
    for (int i = 0; i < files.length; i++) { 
      // 復(fù)制文件 
      if (files[i].isFile()) { 
        flag = CopyFileUtil.copyFile(files[i].getAbsolutePath(), 
            destDirName + files[i].getName(), overlay); 
        if (!flag) 
          break; 
      } else if (files[i].isDirectory()) { 
        flag = CopyFileUtil.copyDirectory(files[i].getAbsolutePath(), 
            destDirName + files[i].getName(), overlay); 
        if (!flag) 
          break; 
      } 
    } 
    if (!flag) { 
      MESSAGE = "復(fù)制目錄" + srcDirName + "至" + destDirName + "失?。?; 
      JOptionPane.showMessageDialog(null, MESSAGE); 
      return false; 
    } else { 
      return true; 
    } 
  } 
 
  public static void main(String[] args) { 
    String srcDirName = "C:/test/test0/test1"; 
    String destDirName = "c:/ttt"; 
    CopyFileUtil.copyDirectory(srcDirName, destDirName, true); 
  } 
} 

不考慮多線程優(yōu)化,單線程文件復(fù)制最快的方法是(文件越大該方法越有優(yōu)勢,一般比常用方法快30+%):

private static void nioTransferCopy(File source, File target) { 
  FileChannel in = null; 
  FileChannel out = null; 
  FileInputStream inStream = null; 
  FileOutputStream outStream = null; 
  try { 
    inStream = new FileInputStream(source); 
    outStream = new FileOutputStream(target); 
    in = inStream.getChannel(); 
    out = outStream.getChannel(); 
    in.transferTo(0, in.size(), out); 
  } catch (IOException e) { 
    e.printStackTrace(); 
  } finally { 
    close(inStream); 
    close(in); 
    close(outStream); 
    close(out); 
  } 
} 

如果需要監(jiān)測復(fù)制進(jìn)度,可以用第二快的方法(留意buffer的大小,對速度有很大影響):

private static void nioBufferCopy(File source, File target) { 
  FileChannel in = null; 
  FileChannel out = null; 
  FileInputStream inStream = null; 
  FileOutputStream outStream = null; 
  try { 
    inStream = new FileInputStream(source); 
    outStream = new FileOutputStream(target); 
    in = inStream.getChannel(); 
    out = outStream.getChannel(); 
    ByteBuffer buffer = ByteBuffer.allocate(4096); 
    while (in.read(buffer) != -1) { 
      buffer.flip(); 
      out.write(buffer); 
      buffer.clear(); 
    } 
  } catch (IOException e) { 
    e.printStackTrace(); 
  } finally { 
    close(inStream); 
    close(in); 
    close(outStream); 
    close(out); 
  } 
} 

常用的方法1是:

private static void customBufferBufferedStreamCopy(File source, File target) { 
  InputStream fis = null; 
  OutputStream fos = null; 
  try { 
    fis = new BufferedInputStream(new FileInputStream(source)); 
    fos = new BufferedOutputStream(new FileOutputStream(target)); 
    byte[] buf = new byte[4096]; 
    int i; 
    while ((i = fis.read(buf)) != -1) { 
      fos.write(buf, 0, i); 
    } 
  } 
  catch (Exception e) { 
    e.printStackTrace(); 
  } finally { 
    close(fis); 
    close(fos); 
  } 
} 

常用的方法2是:

private static void customBufferStreamCopy(File source, File target) { 
  InputStream fis = null; 
  OutputStream fos = null; 
  try { 
    fis = new FileInputStream(source); 
    fos = new FileOutputStream(target); 
    byte[] buf = new byte[4096]; 
    int i; 
    while ((i = fis.read(buf)) != -1) { 
      fos.write(buf, 0, i); 
    } 
  } 
  catch (Exception e) { 
    e.printStackTrace(); 
  } finally { 
    close(fis); 
    close(fos); 
  } 
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • mybatis多個plugins的執(zhí)行順序解析

    mybatis多個plugins的執(zhí)行順序解析

    這篇文章主要介紹了mybatis多個plugins的執(zhí)行順序解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • intellij IDEA配置springboot的圖文教程

    intellij IDEA配置springboot的圖文教程

    Spring Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程。接下來通過本文給大家介紹intellij IDEA配置springboot的圖文教程,感興趣的朋友一起看看吧
    2018-03-03
  • spring?jpa集成依賴的環(huán)境準(zhǔn)備及實(shí)體類倉庫編寫教程

    spring?jpa集成依賴的環(huán)境準(zhǔn)備及實(shí)體類倉庫編寫教程

    這篇文章主要為大家介紹了spring?jpa集成依賴的環(huán)境準(zhǔn)備及實(shí)體類倉庫編寫教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03
  • Java抽象類的概念講解

    Java抽象類的概念講解

    今天小編就為大家分享一篇關(guān)于Java抽象類的概念講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • Java中的this和super實(shí)例淺析

    Java中的this和super實(shí)例淺析

    要說this和super就不得不說Java的封裝和繼承了。這篇文章主要介紹了Java中的this和super實(shí)例淺析,需要的朋友可以參考下
    2017-03-03
  • java開發(fā)之基于Validator接口的SpringMVC數(shù)據(jù)校驗(yàn)方式

    java開發(fā)之基于Validator接口的SpringMVC數(shù)據(jù)校驗(yàn)方式

    這篇文章主要介紹了java開發(fā)之基于Validator接口的SpringMVC數(shù)據(jù)校驗(yàn)方式,文中附含詳細(xì)示例代碼,有需要的朋友可以借鑒參考下
    2021-09-09
  • SpringBoot通過ip獲取歸屬地的幾種方式分享

    SpringBoot通過ip獲取歸屬地的幾種方式分享

    在日常我們逛網(wǎng)站的時候會發(fā)現(xiàn)我們登錄后會出現(xiàn)歸屬地信息,例如:我在廣州登錄會顯示廣東廣州,有些更加精確的會顯示到區(qū)縣,那么我們來看看有哪些方式來獲取歸屬地信息,今天我們來聊一聊
    2023-09-09
  • SpringMVC使用MultipartResolver實(shí)現(xiàn)文件上傳

    SpringMVC使用MultipartResolver實(shí)現(xiàn)文件上傳

    MultipartResolver 用于處理文件上傳,當(dāng)收到請求時 DispatcherServlet 的 checkMultipart() 方法會調(diào)用 MultipartResolver 的 isMultipart() 方法判斷請求中是否包含文件
    2023-02-02
  • MyBatis 接收數(shù)據(jù)庫中沒有的字段的解決

    MyBatis 接收數(shù)據(jù)庫中沒有的字段的解決

    這篇文章主要介紹了MyBatis 接收數(shù)據(jù)庫中沒有的字段的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • java?springboot中如何讀取配置文件的屬性

    java?springboot中如何讀取配置文件的屬性

    大家好,本篇文章主要講的是java?springboot中如何讀取配置文件的屬性,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01

最新評論

克拉玛依市| 沾化县| 临桂县| 沂源县| 云浮市| 株洲县| 建平县| 六盘水市| 射洪县| 武邑县| 东至县| 青川县| 银川市| 武义县| 红原县| 濉溪县| 视频| 东安县| 温泉县| 临海市| 泗阳县| 平顶山市| 九寨沟县| 乐都县| 亚东县| 朔州市| 乌拉特中旗| 维西| 丰台区| 三明市| 临沭县| 上林县| 泰顺县| 盐山县| 遵义市| 怀化市| 嘉荫县| 博客| 中方县| 四平市| 长治县|