一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

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

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");
 }
}

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日产乱码2021永久手机版 | 97视频人人| 12345国产精品高清在线 | 四虎影院免费在线播放 | 欧美理论片手机在线观看片免费 | www久久精品 | 操出水视频 | 久久精品男人影院 | 任我行视频在线观看国语 | 91porny.首页| 国产成人在线播放 | 精品91自产拍在线观看99re | 国产精品igao视频网网址 | 精品国产品香蕉在线观看 | 久久伊人精品青青草原2021 | 日本无卡无吗中文免费 | 亚洲国产情侣偷自在线二页 | 男女福利视频 | 色综合合久久天天综合绕视看 | 亚洲色大成网站www久久九九 | 国产精品一区二区三区久久 | 亚洲www美色 | 涩涩五月天 | 日本动漫黄网站在线观看 | 亚洲偷窥图区色 | bdsm中国精品调教 | 欧美在线视频7777kkkk | 国产精品videosse | 色婷婷婷丁香亚洲综合不卡 | 草草国产成人免费视频 | 国产一区精品视频 | 午夜免费体验30分 | 男人天堂网在线 | 黄情视频 | 亚洲 日韩 自拍 视频一区 | 91大神亚洲影视在线 | 亚洲国产精品久久人人爱 | 天堂69亚洲精品中文字幕 | 王小军怎么了最新消息 | 国产午夜永久福利视频在线观看 | 干b视频在线观看 |