FileUtil.java
7.3 KB
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
package com.xdy.util;
import android.os.Environment;
import android.text.TextUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.UUID;
/**
* @author 蒋洪波
* @version 2.0
* @file FileHelper.java
* @brief 文件操作辅助类
* @date 2017/11/30
* Copyright (c) 2017, 学点云
* All rights reserved.
*/
public class FileUtil {
private static String userID = "haha";
private static String baseFilePath = Environment.getExternalStorageDirectory().toString() + "/filedownloader";
private static String dowloadFilePath = baseFilePath + "/FILETEMP";
/**
* 下载文件的临时路径
*/
private static String tempDirPath = baseFilePath + "/TEMPDir";
private static String[] wrongChars = {
"/", "\\", "*", "?", "<", ">", "\"", "|"};
// 创建文件
public void newFile(File f) {
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 创建目录
*
* @param
*/
public static void newDirFile(File f) {
if (!f.exists()) {
f.mkdirs();
}
}
// 获取一个文件列表的里的总文件大小
public static double getSize(List<String> willupload) {
return (double) getSizeUnitByte(willupload) / (1024 * 1024);
}
/**
* 计算文件的大小,单位是字节
*
* @param willupload
* @return
*/
public static long getSizeUnitByte(List<String> willupload) {
long allfilesize = 0;
for (int i = 0; i < willupload.size(); i++) {
File newfile = new File(willupload.get(i));
if (newfile.exists() && newfile.isFile()) {
allfilesize = allfilesize + newfile.length();
}
}
return allfilesize;
}
/**
* 获取默认文件存放路径
*/
public static String getFileDefaultPath() {
return dowloadFilePath;
}
/**
* 获取下载文件的临时路径
*/
public static String getTempDirPath() {
return tempDirPath;
}
/**
* 复制单个文件
*
* @param oldPath String 原文件路径 如:c:/fqf.txt
* @param newPath String 复制后路径 如:f:/fqf.txt
* @return boolean
*/
public static boolean copyFile(String oldPath, String newPath) {
boolean iscopy = false;
InputStream inStream = null;
FileOutputStream fs = null;
try {
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
inStream = new FileInputStream(oldPath); //读入原文件
fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1024];
while ((byteread = inStream.read(buffer)) != -1) {
fs.write(buffer, 0, byteread);
}
iscopy = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (inStream != null) {
inStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fs != null) {
fs.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return iscopy;
}
public static void setUserID(String newUserID) {
userID = newUserID;
dowloadFilePath = baseFilePath + "/FILETEMP";
tempDirPath = baseFilePath + "/TEMPDir";
}
public static String getUserID() {
return userID;
}
/**
* 过滤附件ID中某些不能存在在文件名中的字符
*/
public static String filterIDChars(String attID) {
if (attID != null) {
for (int i = 0; i < wrongChars.length; i++) {
String c = wrongChars[i];
if (attID.contains(c)) {
attID = attID.replaceAll(c, "");
}
}
}
return attID;
}
/**
* 安全删除文件.
* @param file
* @return
*/
public static boolean deleteFileSafely(File file) {
if (file != null) {
String tmpPath = file.getParent() + File.separator + System.currentTimeMillis();
File tmp = new File(tmpPath);
file.renameTo(tmp);
return tmp.delete();
}
return false;
}
/**
* 获取过滤ID后的文件名
*/
public static String getFilterFileName(String flieName) {
if (flieName == null || "".equals(flieName)) {
return flieName;
}
boolean isNeedFilter = flieName.startsWith("(");
int index = flieName.indexOf(")");
if (isNeedFilter && index != -1) {
int startIndex = index + 1;
int endIndex = flieName.length();
if (startIndex < endIndex) {
return flieName.substring(startIndex, endIndex);
}
}
return flieName;
}
/**
* get file name from url
*
* @param url
* @return
*/
public static String getFileNameByUrl(String url) {
if (TextUtils.isEmpty(url)) {
return null;
}
int index = url.lastIndexOf('?');
int index2 = url.lastIndexOf("/");
if (index > 0 && index2 >= index) {
return UUID.randomUUID().toString();
}
return url.substring(index2 + 1, index < 0 ? url.length() : index);
}
/**
* get file extend name
*
* @param fileName
* @return
*/
public static String getFileExtendName(String fileName) {
if (TextUtils.isEmpty(fileName)) {
return null;
}
int index = fileName.lastIndexOf('.');
if (index < 0) {
return "unknown";
} else {
return fileName.substring(index + 1);
}
}
public static boolean isFileExists(String filePath) {
if (TextUtils.isEmpty(filePath)) {
return false;
}
return new File(filePath).exists();
}
public static String convertStreamToString(InputStream input) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}