source: other-projects/the-macronizer/trunk/src/java/util/FileUtil.java@ 29855

Last change on this file since 29855 was 29855, checked in by davidb, 9 years ago

John's code after refactoring by Tom over the summer of 2014/2015

File size: 3.4 KB
Line 
1
2package util;
3
4import com.glaforge.i18n.io.CharsetToolkit;
5import java.io.File;
6import java.io.IOException;
7
8/**
9 * @author University of Waikato - Te Whare Wānanga o Waikato
10 * @version 1.0
11 * @since 2014-11-20
12 */
13public class FileUtil {
14
15 private final String TMP_FILE_SUFFIX = "mi-tmp-";
16
17 /**
18 * Find the file type of a file. This is based of the file suffix eg(.txt)
19 * @param File file to find out the file type of.
20 * @return String file type.
21 */
22 public static String guessFileType(File file) {
23 int dotPos = file.getName().lastIndexOf(".");
24 return dotPos != -1 ? file.getName().substring(dotPos) : null;
25 }
26
27 /**
28 * Find the file charset encoding that a file used.
29 * @param File file to find out what charset encoding has been used.
30 * @return String file charset.
31 */
32 public static String guessCharsetEncoding(File file) {
33 String charsetEncoding = null;
34 try {
35 charsetEncoding = CharsetToolkit.guessEncoding(file, 4096).name().toLowerCase();
36 } catch (IOException e) {
37 e.printStackTrace();
38 }
39 return charsetEncoding;
40 }
41
42
43 /**
44 * Deletes a file
45 * @param File file to be deleted.
46 * @return boolean is file deleted.
47 */
48 public static boolean deleteFile(File file) {
49 if (file == null || !file.exists()) {
50 return false;
51 } else if (file.isFile()) {
52 return file.delete();
53 } else {
54 File[] listFiles = file.listFiles();
55 for (File listFile : listFiles) {
56 deleteFile(listFile);
57 }
58 return file.delete();
59 }
60 }
61
62 /**
63 * Check to see if a file exists and is valid
64 * @param File file path to be tested. Will throw error if the file dose not exists or is not valid.
65 * @throws IllegalArgumentException if the file is == null
66 * @throws IllegalArgumentException if the file dose not exists
67 * @throws IllegalArgumentException if the file is not a file
68 */
69 public static void checkValidFile(final File file) {
70 if (file == null) {
71 throw new IllegalArgumentException("The file '" + file
72 + "' is null!");
73 }
74 if (!file.exists()) {
75 throw new IllegalArgumentException("The file '" + file
76 + "' does not exist!");
77 }
78 if (!file.isFile()) {
79 throw new IllegalArgumentException("The name '" + file
80 + "' is not a file!");
81 }
82 }
83
84 /**
85 * Check to see if a directory exists and is valid
86 * @param File directory path to be tested. Will throw error if the directory dose not exists or is not valid.\
87 * @throws IllegalArgumentException if the directory is == null
88 * @throws IllegalArgumentException if the directory dose not exists
89 * @throws IllegalArgumentException if the directory is not a directory
90 */
91 public static void checkValidDir(final File dir) {
92 if (dir == null) {
93 throw new IllegalArgumentException("The directory '" + dir
94 + "' is null!");
95 }
96 if (!dir.exists()) {
97 throw new IllegalArgumentException("The directory '" + dir
98 + "' does not exist!");
99 }
100 if (!dir.isDirectory()) {
101 throw new IllegalArgumentException("The name '" + dir
102 + "' is not a directory!");
103 }
104 }
105}
Note: See TracBrowser for help on using the repository browser.