激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java實現文件或文件夾的復制到指定目錄實例

Java實現文件或文件夾的復制到指定目錄實例

2020-09-03 14:07qq_22672291 Java教程

本篇文章主要介紹了Java實現文件或文件夾的復制到指定目錄實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。

整理文檔,搜刮出一個Java實現文件或文件夾的復制到指定目錄的代碼,稍微整理精簡一下做下分享。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
 
public class Test {
  private static int a = 5;
 
  public static void main(String[] args) {
    //需要復制的目標文件或目標文件夾
    String pathname = "C:/Users/likun/Desktop/git_project";
    File file = new File(pathname);
    //復制到的位置
    String topathname = "C:/Users/likun/Desktop/movie";
    File toFile = new File(topathname);
    try {
      copy(file, toFile);
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
 
  public static void copy(File file, File toFile) throws Exception {
    byte[] b = new byte[1024];
    int a;
    FileInputStream fis;
    FileOutputStream fos;
    if (file.isDirectory()) {
      String filepath = file.getAbsolutePath();
      filepath=filepath.replaceAll("\\\\", "/");
      String toFilepath = toFile.getAbsolutePath();
      toFilepath=toFilepath.replaceAll("\\\\", "/");
      int lastIndexOf = filepath.lastIndexOf("/");
      toFilepath = toFilepath + filepath.substring(lastIndexOf ,filepath.length());
      File copy=new File(toFilepath);
      //復制文件夾
      if (!copy.exists()) {
        copy.mkdir();
      }
      //遍歷文件夾
      for (File f : file.listFiles()) {
        copy(f, copy);
      }
    } else {
      if (toFile.isDirectory()) {
        String filepath = file.getAbsolutePath();
        filepath=filepath.replaceAll("\\\\", "/");
        String toFilepath = toFile.getAbsolutePath();
        toFilepath=toFilepath.replaceAll("\\\\", "/");
        int lastIndexOf = filepath.lastIndexOf("/");
        toFilepath = toFilepath + filepath.substring(lastIndexOf ,filepath.length());
         
        //寫文件
        File newFile = new File(toFilepath);
        fis = new FileInputStream(file);
        fos = new FileOutputStream(newFile);
        while ((a = fis.read(b)) != -1) {
          fos.write(b, 0, a);
        }
      } else {
        //寫文件
        fis = new FileInputStream(file);
        fos = new FileOutputStream(toFile);
        while ((a = fis.read(b)) != -1) {
          fos.write(b, 0, a);
        }
      }
 
    }
  }
 
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/qq_22672291/article/details/68064194

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: av在线免费观看网 | 日本不卡中文字幕 | 日本黄色免费片 | 亚洲射逼 | 男女羞羞视频在线免费观看 | 永久免费不卡在线观看黄网站 | 亚洲国产精品久久久久久久久久 | 国产一级不卡毛片 | 久草在线网址 | 在线观看免费毛片视频 | 美女又黄又www | 亚洲91网站 | 福利在线小视频 | 午夜视频在线看 | 亚洲二区不卡 | 在线2区| 91久久在线观看 | 日本免费一区二区三区四区 | 亚洲午夜免费 | 暖暖免费观看高清完整版电影 | 特片网久久 | 91 成人 | 成人免费看毛片 | 日韩一级片毛片 | 日韩不卡一区二区 | 欧美一级视频网站 | 狠狠操夜夜爱 | 色av综合在线 | 久久69精品久久久久久国产越南 | 最新在线黄色网址 | 中国产一级毛片 | av手机在线免费播放 | 一级免费a | 亚洲国产成人一区 | 日韩黄色免费观看 | 91网站链接| 在线成人精品视频 | 国产无区一区二区三麻豆 | 在线播放的av网站 | 久久精精品 | 国产精品一区在线免费观看 |