• <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>

    java文件復制功能實現

          Java是一門面標的目的對象編程說話,不僅接收了C++說話的各類長處,還摒棄了C++里難以理解的多擔當、指針等概念,是以Java說話具有功能壯大和簡單易用兩個特征。Java說話作為靜態面標的目的對象編程說話的代表,極好地實現了面標的目的對象理論,許可程序員以優雅的思維體例進行復雜的編程 。

          Java具有簡單性、面標的目的對象、分布式、健壯性、平安性、平臺自力與可移植性、多線程、動態性等特點 。Java可以編寫桌面應用程序、Web應用程序、分布式系統和嵌入式系統應用程序等  。

    東西/原料

    • 電腦
    • myeclipse和intellij IDEA

    方式/步調

    1. 1

      第一種:根基copy經由過程字節省。

      1、具體代碼如下所示:

      import java.io.BufferedInputStream;

      import java.io.BufferedOutputStream;

      import java.io.ByteArrayOutputStream;

      import java.io.File;

      import java.io.FileInputStream;

      import java.io.FileNotFoundException;

      import java.io.FileOutputStream;

      public class Test8 {

      public static void main(String[] args) {

      ByteArrayOutputStream bos = null;

      BufferedInputStream in = null;

      try {

      File file = new File("D:/Documents/Downloads/新建文件夾 (2)/代辦署理合同.pdf");

      if (!file.exists()) {

      throw new FileNotFoundException("file not exists");

      }

      in = new BufferedInputStream(new FileInputStream(file));

      bos = new ByteArrayOutputStream((int) file.length());

      int buf_size = 1024;

      byte[] buffer = new byte[buf_size];

      int len = 0;

      while (-1 != (len = in.read(buffer, 0, buf_size))) {

      bos.write(buffer, 0, len);

      }

      copyFile(bos.toByteArray(), "d:/test.pdf");

      System.out.println(bos.toByteArray());

      } catch (Exception e) {}

      }

      public static void copyFile(byte[] fileByte, String filePath)

      throws Exception {

      File file = new File(filePath);

      FileOutputStream fs = new FileOutputStream(file);

      BufferedOutputStream bo = new BufferedOutputStream(fs);

      bo.write(fileByte);

      bo.close();

      }

      }

    2. 2

      第二種:借助于java.nio.channels.FileChannel實現復制文件。

      代碼如下所示:

      import java.io.File;

      import java.io.FileInputStream;

      import java.io.FileOutputStream;

      import java.io.IOException;

      import java.nio.channels.FileChannel;

      public class Test8 {

      public static void main(String[] args) throws Exception {

      File source = new File("d:/test.pdf");

      if (source.exists()) {

      File dest = new File("d:/test2.pdf");

      copyFileChannels(source, dest);

      } else {

      System.out.println("原文件不存在!");

      }

      }

      public static void copyFileChannels(File source, File dest)

      throws IOException {

      FileChannel inputChannel = null;

      FileChannel outputChannel = null;

      try {

      inputChannel = new FileInputStream(source).getChannel();

      outputChannel = new FileOutputStream(dest).getChannel();

      outputChannel.transferFrom(inputChannel, 0, inputChannel.size());

      } finally {

      inputChannel.close();

      outputChannel.close();

      }

      }

      }

    3. 3

      第三種:利用java7之后供給的java.nio.file.Files實現。

      代碼如下:

      import java.io.*;

      import java.nio.file.Files;

      public class CopyTest {

      public static void main(String[] args) {

      File inFile = new File("E:/圖片/捉妖記.jpg");

      File outFile = new File("E:/file/捉妖記.jpg");

      try {

      Files.copy(inFile.toPath(), outFile.toPath());

      } catch (IOException e) {

      e.printStackTrace();

      }

      }

      }

    4. 4

      第四種:借助于Apache辦事器供給類org.apache.commons.io.FileUtils

      1、類在架包commons-io.jar中

      2、下載架包

      2.1 百度搜刮:commons-io.jar下載

      2.2 

      3、具體實現代碼:

      import java.io.File;

      import org.apache.commons.io.FileUtils;

      public class Test8 {

      public static void main(String[] args) throws Exception {

      File source = new File("d:/test.pdf");

      if (source.exists()) {

      File dest = new File("d:/test3.pdf");

      FileUtils.copyFile(source, dest);

      } else {

      System.out.println("原文件不存在!");

      }

      }

      }

    注重事項

    • jdk 1.6 ,jdk 1.8 myeclipse 2010 ,IDEA 2018.2.2
    • 上面復制文件的體例,若是文件名字已經存在則籠蓋已有文件
    • 發表于 2019-05-10 20:01
    • 閱讀 ( 978 )
    • 分類:其他類型

    你可能感興趣的文章

    相關問題

    0 條評論

    請先 登錄 后評論
    admin
    admin

    0 篇文章

    作家榜 ?

    1. xiaonan123 189 文章
    2. 湯依妹兒 97 文章
    3. luogf229 46 文章
    4. jy02406749 45 文章
    5. 小凡 34 文章
    6. Daisy萌 32 文章
    7. 我的QQ3117863681 24 文章
    8. 華志健 23 文章

    聯系我們:uytrv@hotmail.com 問答工具
  • <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>
    久久久久精品国产麻豆