1
2
3
4
5 package gboat2.base.bridge.util;
6
7 import gboat2.base.bridge.GboatAppConstants;
8 import gboat2.base.bridge.exception.DefaultGboatNestedException;
9
10 import java.io.BufferedInputStream;
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.RandomAccessFile;
16 import java.net.URL;
17 import java.nio.channels.FileChannel;
18 import java.nio.channels.FileLock;
19
20 import org.apache.commons.io.FileUtils;
21 import org.apache.commons.io.IOUtils;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25
26
27
28
29
30
31
32
33 public class FileUtil {
34 public static final Logger logger = LoggerFactory.getLogger(FileUtil.class);
35
36
37
38
39
40
41 public static boolean exists(String path) {
42 return new File(path).exists();
43 }
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public static String loadAsString(URL url) {
58 return loadAsString(url, GboatAppConstants.ENCODING_UTF8);
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public static String loadAsString(URL url, String charset) {
74 try {
75 return IOUtils.toString(url, charset);
76 } catch (IOException e) {
77 throw new DefaultGboatNestedException("读取 URL [" + url + "] 的内容失败", e);
78 }
79 }
80
81
82
83
84
85
86
87 public static String loadAsString(String fullpath) {
88 return loadAsString(fullpath, GboatAppConstants.ENCODING_UTF8);
89 }
90
91
92
93
94
95
96
97
98 public static String loadAsString(String filePath, String charset) {
99 try {
100 return FileUtils.readFileToString(new File(filePath), charset);
101 } catch (IOException e) {
102 throw new DefaultGboatNestedException("读取文件 [" + filePath + "] 的内容失败", e);
103 }
104 }
105
106
107
108
109
110
111
112 public static String loadAsString(InputStream in) {
113 return loadAsString(in, GboatAppConstants.ENCODING_UTF8);
114 }
115
116
117
118
119
120
121
122
123 public static String loadAsString(InputStream in, String charset) {
124 try {
125 return IOUtils.toString(in, charset);
126 } catch (IOException e) {
127 throw new DefaultGboatNestedException("读取输入流的字符内容失败", e);
128 }
129 }
130
131
132
133
134
135
136
137
138 public static void writeFileToDisk(File inFile, File outFile) throws IOException{
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 FileUtils.copyFile(inFile, outFile);
154 }
155
156
157
158
159
160
161
162
163 public static void appendFile(File destFile, File srcFile) throws IOException {
164 BufferedInputStream input = null;
165 RandomAccessFile randomAccessFile = null;
166 FileChannel fileChannel = null;
167 try {
168 input = new BufferedInputStream(new FileInputStream(srcFile));
169
170 randomAccessFile = new RandomAccessFile(destFile, "rw");
171 fileChannel = randomAccessFile.getChannel();
172 FileLock fileLock = fileChannel.tryLock();
173
174 if (fileLock != null) {
175 randomAccessFile.seek(destFile.length());
176 }
177
178 byte[] bytes = new byte[1024000];
179 int realLength = 0;
180 while ((realLength = input.read(bytes, 0, bytes.length)) != -1) {
181 randomAccessFile.write(bytes, 0, realLength);
182 }
183
184 if (fileLock != null) {
185 fileLock.release();
186 fileLock = null;
187 }
188 } finally {
189 IOUtils.closeQuietly(fileChannel);
190 IOUtils.closeQuietly(randomAccessFile);
191 IOUtils.closeQuietly(input);
192 }
193 }
194 }