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

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

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Java文件操作工具類(lèi)fileUtil實(shí)例【文件增刪改,復(fù)制等】

Java文件操作工具類(lèi)fileUtil實(shí)例【文件增刪改,復(fù)制等】

2021-01-28 11:41lovoo Java教程

這篇文章主要介紹了Java文件操作工具類(lèi)fileUtil,結(jié)合實(shí)例形式分析了java針對(duì)文件進(jìn)行讀取、增加、刪除、修改、復(fù)制等操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了Java文件操作工具類(lèi)fileUtil。分享給大家供大家參考,具體如下:

?
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package com.gcloud.common;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
/**
 * 文件工具類(lèi)
 * Created by charlin on 2017/9/8.
 */
public class FileUtil {
  /**
   * 讀取文件內(nèi)容
   *
   * @param is
   * @return
   */
  public static String readFile(InputStream is) {
    BufferedReader br = null;
    StringBuffer sb = new StringBuffer();
    try {
      br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
      String readLine = null;
      while ((readLine = br.readLine()) != null) {
        sb.append(readLine);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        br.close();
        is.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return sb.toString();
  }
  /**
   * 判斷指定的文件是否存在。
   *
   * @param fileName
   * @return
   */
  public static boolean isFileExist(String fileName) {
    return new File(fileName).isFile();
  }
  /**
   * 創(chuàng)建指定的目錄。 如果指定的目錄的父目錄不存在則創(chuàng)建其目錄書(shū)上所有需要的父目錄。
   * 注意:可能會(huì)在返回false的時(shí)候創(chuàng)建部分父目錄。
   *
   * @param file
   * @return
   */
  public static boolean makeDirectory(File file) {
    File parent = file.getParentFile();
    if (parent != null) {
      return parent.mkdirs();
    }
    return false;
  }
  /**
   * 返回文件的URL地址。
   *
   * @param file
   * @return
   * @throws MalformedURLException
   */
  public static URL getURL(File file) throws MalformedURLException {
    String fileURL = "file:/" + file.getAbsolutePath();
    URL url = new URL(fileURL);
    return url;
  }
  /**
   * 從文件路徑得到文件名。
   *
   * @param filePath
   * @return
   */
  public static String getFileName(String filePath) {
    File file = new File(filePath);
    return file.getName();
  }
  /**
   * 從文件名得到文件絕對(duì)路徑。
   *
   * @param fileName
   * @return
   */
  public static String getFilePath(String fileName) {
    File file = new File(fileName);
    return file.getAbsolutePath();
  }
  /**
   * 將DOS/Windows格式的路徑轉(zhuǎn)換為UNIX/Linux格式的路徑。
   *
   * @param filePath
   * @return
   */
  public static String toUNIXpath(String filePath) {
    return filePath.replace("", "/");
  }
  /**
   * 從文件名得到UNIX風(fēng)格的文件絕對(duì)路徑。
   *
   * @param fileName
   * @return
   */
  public static String getUNIXfilePath(String fileName) {
    File file = new File(fileName);
    return toUNIXpath(file.getAbsolutePath());
  }
  /**
   * 得到文件后綴名
   *
   * @param fileName
   * @return
   */
  public static String getFileExt(String fileName) {
    int point = fileName.lastIndexOf('.');
    int length = fileName.length();
    if (point == -1 || point == length - 1) {
      return "";
    } else {
      return fileName.substring(point + 1, length);
    }
  }
  /**
   * 得到文件的名字部分。 實(shí)際上就是路徑中的最后一個(gè)路徑分隔符后的部分。
   *
   * @param fileName
   * @return
   */
  public static String getNamePart(String fileName) {
    int point = getPathLastIndex(fileName);
    int length = fileName.length();
    if (point == -1) {
      return fileName;
    } else if (point == length - 1) {
      int secondPoint = getPathLastIndex(fileName, point - 1);
      if (secondPoint == -1) {
        if (length == 1) {
          return fileName;
        } else {
          return fileName.substring(0, point);
        }
      } else {
        return fileName.substring(secondPoint + 1, point);
      }
    } else {
      return fileName.substring(point + 1);
    }
  }
  /**
   * 得到文件名中的父路徑部分。 對(duì)兩種路徑分隔符都有效。 不存在時(shí)返回""。
   * 如果文件名是以路徑分隔符結(jié)尾的則不考慮該分隔符,例如"/path/"返回""。
   *
   * @param fileName
   * @return
   */
  public static String getPathPart(String fileName) {
    int point = getPathLastIndex(fileName);
    int length = fileName.length();
    if (point == -1) {
      return "";
    } else if (point == length - 1) {
      int secondPoint = getPathLastIndex(fileName, point - 1);
      if (secondPoint == -1) {
        return "";
      } else {
        return fileName.substring(0, secondPoint);
      }
    } else {
      return fileName.substring(0, point);
    }
  }
  /**
   * 得到路徑分隔符在文件路徑中最后出現(xiàn)的位置。 對(duì)于DOS或者UNIX風(fēng)格的分隔符都可以。
   *
   * @param fileName
   * @return
   */
  public static int getPathLastIndex(String fileName) {
    int point = fileName.lastIndexOf("/");
    if (point == -1) {
      point = fileName.lastIndexOf("");
    }
    return point;
  }
  /**
   * 得到路徑分隔符在文件路徑中指定位置前最后出現(xiàn)的位置。 對(duì)于DOS或者UNIX風(fēng)格的分隔符都可以。
   *
   * @param fileName
   * @param fromIndex
   * @return
   */
  public static int getPathLastIndex(String fileName, int fromIndex) {
    int point = fileName.lastIndexOf("/", fromIndex);
    if (point == -1) {
      point = fileName.lastIndexOf("", fromIndex);
    }
    return point;
  }
  /**
   * 得到路徑分隔符在文件路徑中首次出現(xiàn)的位置。 對(duì)于DOS或者UNIX風(fēng)格的分隔符都可以。
   *
   * @param fileName
   * @return
   */
  public static int getPathIndex(String fileName) {
    int point = fileName.indexOf("/");
    if (point == -1) {
      point = fileName.indexOf("");
    }
    return point;
  }
  /**
   * 得到路徑分隔符在文件路徑中指定位置后首次出現(xiàn)的位置。 對(duì)于DOS或者UNIX風(fēng)格的分隔符都可以。
   *
   * @param fileName
   * @param fromIndex
   * @return
   */
  public static int getPathIndex(String fileName, int fromIndex) {
    int point = fileName.indexOf("/", fromIndex);
    if (point == -1) {
      point = fileName.indexOf("", fromIndex);
    }
    return point;
  }
  /**
   * 將文件名中的類(lèi)型部分去掉。
   *
   * @param filename
   * @return
   */
  public static String removeFileExt(String filename) {
    int index = filename.lastIndexOf(".");
    if (index != -1) {
      return filename.substring(0, index);
    } else {
      return filename;
    }
  }
  /**
   * 得到相對(duì)路徑。 文件名不是目錄名的子節(jié)點(diǎn)時(shí)返回文件名。
   *
   * @param pathName
   * @param fileName
   * @return
   */
  public static String getSubpath(String pathName, String fileName) {
    int index = fileName.indexOf(pathName);
    if (index != -1) {
      return fileName.substring(index + pathName.length() + 1);
    } else {
      return fileName;
    }
  }
  /**
   * 刪除一個(gè)文件。
   *
   * @param filename
   * @throws IOException
   */
  public static void deleteFile(String filename) throws IOException {
    File file = new File(filename);
    if (file.isDirectory()) {
      throw new IOException("IOException -> BadInputException: not a file.");
    }
    if (!file.exists()) {
      throw new IOException("IOException -> BadInputException: file is not exist.");
    }
    if (!file.delete()) {
      throw new IOException("Cannot delete file. filename = " + filename);
    }
  }
  /**
   * 刪除文件夾及其下面的子文件夾
   *
   * @param dir
   * @throws IOException
   */
  public static void deleteDir(File dir) throws IOException {
    if (dir.isFile())
      throw new IOException("IOException -> BadInputException: not a directory.");
    File[] files = dir.listFiles();
    if (files != null) {
      for (int i = 0; i < files.length; i++) {
        File file = files[i];
        if (file.isFile()) {
          file.delete();
        } else {
          deleteDir(file);
        }
      }
    }
    dir.delete();
  }
  /**
   * 復(fù)制文件
   *
   * @param src
   * @param dst
   * @throws Exception
   */
  public static void copy(File src, File dst) throws Exception {
    int BUFFER_SIZE = 4096;
    InputStream in = null;
    OutputStream out = null;
    try {
      in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
      out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);
      byte[] buffer = new byte[BUFFER_SIZE];
      int len = 0;
      while ((len = in.read(buffer)) > 0) {
        out.write(buffer, 0, len);
      }
    } catch (Exception e) {
      throw e;
    } finally {
      if (null != in) {
        try {
          in.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
        in = null;
      }
      if (null != out) {
        try {
          out.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
        out = null;
      }
    }
  }
  /**
   * @復(fù)制文件,支持把源文件內(nèi)容追加到目標(biāo)文件末尾
   * @param src
   * @param dst
   * @param append
   * @throws Exception
   */
  public static void copy(File src, File dst, boolean append) throws Exception {
    int BUFFER_SIZE = 4096;
    InputStream in = null;
    OutputStream out = null;
    try {
      in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
      out = new BufferedOutputStream(new FileOutputStream(dst, append), BUFFER_SIZE);
      byte[] buffer = new byte[BUFFER_SIZE];
      int len = 0;
      while ((len = in.read(buffer)) > 0) {
        out.write(buffer, 0, len);
      }
    } catch (Exception e) {
      throw e;
    } finally {
      if (null != in) {
        try {
          in.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
        in = null;
      }
      if (null != out) {
        try {
          out.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
        out = null;
      }
    }
  }
}

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

原文鏈接:http://blog.csdn.net/lovoo/article/details/77899627

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产视频一二三区 | а√天堂中文在线资源8 | 99r精品在线 | 日本 欧美 国产 | 成人3d动漫一区二区三区91 | 色噜噜视频 | 成人在线精品视频 | 日韩国产一区二区三区 | 成人午夜电影网 | 国产成人免费 | 综合久久久 | 日韩欧美手机在线 | 国产免费激情视频 | 日韩激情一区二区三区 | 日韩免费在线观看 | 精品二区 | 香蕉av777xxx色综合一区 | 国产精品久久久久久久久久东京 | 国外精品久久久蜜桃免费全文阅读 | 久久国产免费 | 免费成年人视频在线观看 | 欧美日韩成人 | 欧美第5页 | 成人av在线网 | 免费日韩成人 | 欧美区视频 | 国产精品99久久久久久动医院 | 日韩成人在线电影 | 中文字幕av一区二区三区 | 久久久久国产一区二区三区四区 | 91羞羞网站 | 久久色av| 日韩精品久久久 | 日韩不卡一区二区三区 | 美女搞黄网站 | 91成人看片| 亚洲视频区| 久久久99精品免费观看 | 4h影院| 日韩成人精品在线 | 国产精品一区二区三区免费 |