国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達(dá)式|

服務(wù)器之家 - 編程語言 - JAVA教程 - java實(shí)現(xiàn)文件復(fù)制上傳操作

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

2020-07-08 14:21qq_27298687 JAVA教程

這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)文件復(fù)制上傳操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

java" id="highlighter_915983">
?
1
2
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

代碼:

?
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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ù)制單個(gè)文件
   *
   * @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 + "不是一個(gè)文件!";
      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)文件是目錄還是單個(gè)文件
        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ù)制整個(gè)目錄的內(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)勢(shì),一般比常用方法快30+%):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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)測(cè)復(fù)制進(jìn)度,可以用第二快的方法(留意buffer的大小,對(duì)速度有很大影響):

?
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
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是:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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是:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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);
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产成人精品一区二区 | 亚洲美女性视频 | 欧美日韩精品一区二区三区 | 黄色av网站免费看 | 国产精品美女久久久久久久久久久 | 日本淫片| 成人午夜在线播放 | 欧美在线综合 | 欧美成人激情视频 | 99国产精品久久久久久久成人热 | 中文字幕精品一区 | 久久久91精品国产一区二区三区 | 亚洲免费影院 | 国产精品69毛片高清亚洲 | 色视频在线免费观看 | 午夜天堂精品久久久久 | 日本黄色a视频 | 自拍小电影| 一级毛片国产 | 国产福利在线视频 | 久久久久久国产精品高清 | 日韩在线精品 | 久久人人爽人人爽 | 日韩久久久久久 | www.涩涩视频 | 久久精品国产亚洲一区二区三区 | 一区二区不卡视频 | 在线观看成人av | 午夜日韩 | 在线视频不卡一区 | 久草福利在线视频 | 亚洲精品高潮呻吟久久av | 日日干夜夜操 | www亚洲精品 | av伊人网 | 国产精品18久久久久久久久久久久 | 午夜视频在线播放 | 亚洲第一视频网站 | 午夜影院在线观看 | 毛片免费电影 | 国产精品永久久久久久久久久 |