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

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

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

服務器之家 - 編程語言 - Java教程 - java文件和目錄的增刪復制

java文件和目錄的增刪復制

2020-11-18 10:50tlnshuju 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package com.wangpeng.utill;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.PrintWriter;
 
/**
 * @author wangpeng
 *
 */
public class ToolOfFile {
 
 /**
 * 創建目錄
 *
 * @param folderPath
 *      目錄目錄
 * @throws Exception
 */
 public static void newFolder(String folderPath) throws Exception {
 try {
  java.io.File myFolder = new java.io.File(folderPath);
  if (!myFolder.exists()) {
  myFolder.mkdir();
  }
 } catch (Exception e) {
  throw e;
 }
 }
 
 /**
 * 創建文件
 *
 * @param filePath
 *      文件路徑
 * @throws Exception
 */
 public static void newFile(String filePath) throws Exception {
 try {
  File myFile = new File(filePath);
  if (!myFile.exists()) {
  myFile.createNewFile();
  }
 } catch (Exception e) {
  throw e;
 }
 }
 
 /**
 * 創建文件,并寫入內容
 *
 * @param filePath
 *      文件路徑
 * @param fileContent
 *      被寫入的文件內容
 * @throws Exception
 */
 public static void newFile(String filePath, String fileContent)
  throws Exception {
 // 用來寫入字符文件的便捷類
 FileWriter fileWriter = null;
 // 向文本輸出流打印對象的格式化表示形式,使用指定文件創建不具有自己主動行刷新的新
 PrintWriter printWriter = null;
 try {
  File myFile = new File(filePath);
  if (!myFile.exists()) {
  myFile.createNewFile();
  }
 
  fileWriter = new FileWriter(myFile);
  printWriter = new PrintWriter(fileWriter);
 
  printWriter.print(fileContent);
  printWriter.flush();
 } catch (Exception e) {
  throw e;
 } finally {
  if (printWriter != null) {
  printWriter.close();
  }
  if (fileWriter != null) {
  fileWriter.close();
  }
 }
 }
 
 /**
 * 復制文件
 *
 * @param oldPath
 *      被拷貝的文件
 * @param newPath
 *      復制到的文件
 * @throws Exception
 */
 public static void copyFile(String oldPath, String newPath)
  throws Exception {
 InputStream inStream = null;
 FileOutputStream outStream = null;
 try {
  int byteread = 0;
  File oldfile = new File(oldPath);
  // 文件存在時
  if (oldfile.exists()) {
  inStream = new FileInputStream(oldfile);
  outStream = new FileOutputStream(newPath);
 
  byte[] buffer = new byte[1444];
  while ((byteread = inStream.read(buffer)) != -1) {
   outStream.write(buffer, 0, byteread);
  }
  outStream.flush();
  }
 } catch (Exception e) {
  throw e;
 } finally {
  if (outStream != null) {
  outStream.close();
  }
  if (inStream != null) {
  inStream.close();
  }
 }
 }
 
 /**
 * 復制文件
 * @param inStream 被拷貝的文件的輸入流
 * @param newPath 被復制到的目標
 * @throws Exception
 */
 public static void copyFile(InputStream inStream, String newPath)
  throws Exception {
 FileOutputStream outStream = null;
 try {
  int byteread = 0;
  outStream = new FileOutputStream(newPath);
  byte[] buffer = new byte[1444];
  while ((byteread = inStream.read(buffer)) != -1) {
  outStream.write(buffer, 0, byteread);
  }
  outStream.flush();
 } catch (Exception e) {
  throw e;
 } finally {
  if (outStream != null) {
  outStream.close();
  }
  if (inStream != null) {
  inStream.close();
  }
 }
 }
 
 /**
 * 復制目錄
 *
 * @param oldPath
 *      被復制的目錄路徑
 * @param newPath
 *      被復制到的目錄路徑
 * @throws Exception
 */
 public static void copyFolder(String oldPath, String newPath)
  throws Exception {
 try {
  (new File(newPath)).mkdirs(); // 假設目錄不存在 則建立新目錄
  File a = new File(oldPath);
  String[] file = a.list();
  File tempIn = null;
  for (int i = 0; i < file.length; i++) {
  if (oldPath.endsWith(File.separator)) {
   tempIn = new File(oldPath + file[i]);
  } else {
   tempIn = new File(oldPath + File.separator + file[i]);
  }
 
  if (tempIn.isFile()) {
   copyFile(tempIn.getAbsolutePath(),
    newPath + "/" + (tempIn.getName()).toString());
  } else if (tempIn.isDirectory()) {// 假設是子目錄
   copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
  }
  }
 } catch (Exception e) {
  throw e;
 }
 }
 
 /**
 * 刪除文件
 *
 * @param filePathAndName
 */
 public static void delFileX(String filePathAndName) {
 File myDelFile = new File(filePathAndName);
 myDelFile.delete();
 }
 
 /**
 * 刪除目錄
 *
 * @param path
 */
 public static void delForder(String path) {
 File delDir = new File(path);
 if (!delDir.exists()) {
  return;
 }
 if (!delDir.isDirectory()) {
  return;
 }
 String[] tempList = delDir.list();
 File temp = null;
 for (int i = 0; i < tempList.length; i++) {
  if (path.endsWith(File.separator)) {
  temp = new File(path + tempList[i]);
  } else {
  temp = new File(path + File.separator + tempList[i]);
  }
 
  if (temp.isFile()) {
  temp.delete();
  } else if (temp.isDirectory()) {
  // 刪除完里面全部內容
  delForder(path + "/" + tempList[i]);
  }
 }
 delDir.delete();
 }
 
 public static void main(String[] args) {
 String oldPath = "F:/test/aaa/";
 String newPath = "F:/test/bbb/";
 
 try {
  // ToolOfFile.newFolder("F:/test/hello/");
  // ToolOfFile.newFile("F:/test/hello/world.txt","我愛你,the world!
");
  ToolOfFile.copyFolder(oldPath, newPath);
  // ToolOfFile.delForder("F:/test/hello");
 } catch (Exception e) {
  e.printStackTrace();
 }
 System.out.println("OK");
 }
}

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

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25
主站蜘蛛池模板: 最新中文字幕免费视频 | 午夜爱爱福利 | 国产一区二区三区在线免费观看 | 最新中文字幕第一页视频 | 午夜小视频免费观看 | 久久精品.com | 久久男人天堂 | 91亚洲精品一区二区福利 | 538在线精品 | 国产精品国产三级国产aⅴ无密码 | japan护士性xxxⅹhd | 精品久久久久久中文字幕 | 妇女毛片| 日本在线不卡一区二区 | 国产一级二级在线播放 | 欧美一级免费在线观看 | 国产一级淫片a级aaa | 一区二区三区欧美日韩 | 精品国产一区二区久久 | 青草视频在线观看视频 | 亚洲精品久久久久久下一站 | 欧美成人免费小视频 | 黄色羞羞视频在线观看 | 蜜桃传媒视频麻豆第一区免费观看 | 神马福利网 | 亚洲人成网站免费播放 | 欧美日本另类 | 国产一区影院 | 在线播放免费播放av片 | 精品久久久久久久久久久下田 | 色8久久| 国内精品久久久久久2021浪潮 | 国产精品午夜未成人免费观看 | 一区二区三区四区五区中文字幕 | 国产免费一区视频 | 特黄一区二区三区 | 黄色特级视频 | 久久精品av| 欧美成人一区免费视频 | 99亚洲| 久久精品视频1 |