1
1
/**
2
2
* Common FileUtils.
3
- *
4
3
* @author: omegaui
5
4
* Copyright (C) 2023 Omega UI
6
- * <p>
5
+
7
6
* This program is free software: you can redistribute it and/or modify
8
7
* it under the terms of the GNU General Public License as published by
9
8
* the Free Software Foundation, either version 3 of the License, or
10
9
* (at your option) any later version.
11
- * <p>
10
+
12
11
* This program is distributed in the hope that it will be useful,
13
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
14
* GNU General Public License for more details.
16
- * <p>
15
+
17
16
* You should have received a copy of the GNU General Public License
18
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
18
*/
@@ -35,45 +34,43 @@ public class FileUtils {
35
34
* @param paths array of path
36
35
* @return path
37
36
*/
38
- public static String join (String ... paths ) {
37
+ public static String join (String ... paths ){
39
38
StringBuilder result = new StringBuilder ();
40
- for (String path : paths ) {
39
+ for (String path : paths ){
41
40
result .append (path ).append (File .separator );
42
41
}
43
42
return result .substring (0 , result .length () - 1 );
44
43
}
45
-
46
44
/**
47
45
* Reads and returns the content of a text file.
48
46
* @param file File Object
49
47
* @return String contents
50
48
*/
51
- public static String read (File file ) {
52
- if (!file .exists ()) {
49
+ public static String read (File file ){
50
+ if (!file .exists ()){
53
51
return null ;
54
52
}
55
- StringBuilder content = new StringBuilder () ;
53
+ String content = "" ;
56
54
try (Scanner reader = new Scanner (file )) {
57
- while (reader .hasNextLine ()) {
58
- content . append ( reader .nextLine ()). append ( "\n " ) ;
55
+ while (reader .hasNextLine ()){
56
+ content += reader .nextLine () + "\n " ;
59
57
}
60
58
} catch (FileNotFoundException e ) {
61
59
throw new RuntimeException (e );
62
60
}
63
- return content . toString () ;
61
+ return content ;
64
62
}
65
-
66
63
/**
67
64
* Writes content to a text file.
68
65
* Returns true on successful write operation.
69
66
* @param file File Object
70
67
* @param content content of the file
71
68
* @return boolean
72
69
*/
73
- public static boolean write (File file , String content ) {
70
+ public static boolean write (File file , String content ){
74
71
File parent = file .getParentFile ();
75
- if (parent != null ) {
76
- if (!parent .mkdirs ()) {
72
+ if (parent != null ) {
73
+ if (!parent .mkdirs ()){
77
74
System .err .println ("Cannot construct the parent directories: " + file );
78
75
return false ;
79
76
}
0 commit comments