FileUtils⼯具类的使⽤⽅法FileUtils
获取系统的临时⽬录路径:getTempDirectoryPath()
[java]
1. public static String getTempDirectoryPath() {
2.
3. Property("pdir");
4.
5. }
获取代表系统临时⽬录的⽂件:getTempDirectory ()
[java]
1. public static File getTempDirectory() {
2.
3. return new File(getTempDirectoryPath());
4.
5. }
获取⽤户的主⽬录路径:getUserDirectoryPath()
[java]
1. public static String getUserDirectoryPath() {
2.
3. Property("user.home");
4.
5. }
获取代表⽤户主⽬录的⽂件:getUserDirectory()
[java]
1. public static File getUserDirectory() {
2.
3. return new File(getUserDirectoryPath());
4.
5. }
根据指定的⽂件获取⼀个新的⽂件输⼊流:openInputStream(File file)
[java]
1. public static FileInputStream openInputStream(File file) throws IOException {
2.
3. if (ists()) {
4.
5. if (file.isDirectory()) {
6.
7. throw new IOException("File '" + file + "' exists but is adirectory");
8.
9. }
12.
13. throw new IOException("File '" + file + "' cannot be read");
14.
15. }
16.
17. } else {
18.
19. throw newFileNotFoundException("File '" + file + "' does notexist");
20.
21. }
22.
23. return new FileInputStream(file);
24.
25. }
根据指定的⽂件获取⼀个新的⽂件输出流:openOutputStream (File file)
[java]
1. public static FileOutputStream openOutputStream(File file) throws IOException {
2.
3. if (ists()) {
4.
5. if (file.isDirectory()) {
6.
7. throw new IOException("File'" + file + "' exists but is a directory");
8.
9. }
10.
11. if (file.canWrite() == false) {
12.
13. throw new IOException("File '" + file + "' cannot be written to");
14.
15. }
16.
17. } else {
18.
19. File parent = ParentFile();
20.
21. if (parent != null &&ists() == false) {
22.
23. if (parent.mkdirs() ==false) {
24.
25. throw new IOException("File '" + file + "' could not be created");
26.
27. }
28.
29. }
30.
31. }
32.
33. return new FileOutputStream(file);
字节转换成直观带单位的值(包括单位GB,MB,KB或字节)byteCountToDisplaySize(long size)
[java]
1. public static StringbyteCountToDisplaySize(long size) {
2.
3. String displaySize;
4.
5. if (size / ONE_GB > 0) {
6.
7. displaySize =String.valueOf(size / ONE_GB) + " GB";
8.
9. } else if (size / ONE_MB > 0) {
10.
11. displaySize =String.valueOf(size / ONE_MB) + " MB";
12.
13. } else if (size / ONE_KB > 0) {
14.
15. displaySize =String.valueOf(size / ONE_KB) + " KB";
16.
17. } else {
18.
19. displaySize =String.valueOf(size) + " bytes";
20.
21. }
22.
23. return displaySize;
24.
25. }
创建⼀个空⽂件,若⽂件应经存在则只更改⽂件的最近修改时间:touch(File file)
[java]
1. public static void touch(File file) throws IOException {
2.
3. if (!ists()) {
4.
5. OutputStream out =openOutputStream(file);
6.
7. IOUtils.closeQuietly(out);
8.
9. }
10.
11. boolean success =file.setLastModified(System.currentTimeMillis());
12.
13. if (!success) {
14.
15. throw new IOException("Unableto set the last modification time for " + file);
16.
17. }
把相应的⽂件集合转换成⽂件数组convertFileCollectionToFileArray(Collection<File> files)
[java]
1. public static File[] convertFileCollectionToFileArray(Collection<File> files) {
2.
3. Array(newFile[files.size()]);
4.
5. }
根据⼀个过滤规则获取⼀个⽬录下的⽂件innerListFiles(Collection<File> files, File directory,IOFileFilterfilter)
[java]
1. private static void innerListFiles(Collection<File> files, File directory,
2.
3. IOFileFilter filter) {
4.
5. File[] found =directory.listFiles((FileFilter) filter);
6.
7. if (found != null) {
8.
9. for (File file : found) {
10.
11. if (file.isDirectory()) {
12.
13. innerListFiles(files,file, filter);
14.
15. } else {
16.
17. files.add(file);
18.
19. }
20.
21. }
22.
23. }
24.
25. }
根据⼀个IOFileFilter过滤规则获取⼀个⽬录下的⽂件集合listFiles( File directory, IOFileFilterfileFilter, IOFileFilter dirFilter) [java]
1. public static Collection<File> listFiles(
2.
3. File directory, IOFileFilterfileFilter, IOFileFilter dirFilter) {
4.
5. if (!directory.isDirectory()) {
6.
7. throw newIllegalArgumentException(
10.
11. }
12.
13. if (fileFilter == null) {
14.
15. throw newNullPointerException("Parameter 'fileFilter' is null");
16.
17. }
18.
19.
20.
21. //Setup effective file filter
22.
23. IOFileFilter effFileFilter =FileFilterUtils.and(fileFilter,
24.
25. FileFilter(DirectoryFileFilter.INSTANCE));
26.mkdirs方法
27.
28.
29. //Setup effective directory filter
30.
31. IOFileFilter effDirFilter;
32.
33. if (dirFilter == null) {
34.
35. effDirFilter =FalseFileFilter.INSTANCE;
36.
37. } else {
38.
39. effDirFilter =FileFilterUtils.and(dirFilter,
40.
41. DirectoryFileFilter.INSTANCE);
42.
43. }
44.
45.
46.
47. //Find files
48.
49. Collection<File> files = newjava.util.LinkedList<File>();
50.
51. innerListFiles(files, directory,
52.
53. (effFileFilter, effDirFilter));
54.
55. return files;
56.
57. }
根据⼀个IOFileFilter过滤规则获取⼀个⽬录下的⽂件集合的Iterator迭代器iterateFiles( File directory, IOFileFilterfileFilter, IOFileFilter dirFilter)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论