FileUtil.java
6.67 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
package cn.com.polysoft.utils;
import org.springframework.util.StringUtils;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 文件读取工具类
*/
public class FileUtil {
/**
* 读取文件内容,作为字符串返回
*/
public static String readFileAsString(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException(filePath);
}
if (file.length() > 1024 * 1024 * 1024) {
throw new IOException("File is too large");
}
StringBuilder sb = new StringBuilder((int) (file.length()));
// 创建字节输入流
FileInputStream fis = new FileInputStream(filePath);
// 创建一个长度为10240的Buffer
byte[] bbuf = new byte[10240];
// 用于保存实际读取的字节数
int hasRead = 0;
while ( (hasRead = fis.read(bbuf)) > 0 ) {
sb.append(new String(bbuf, 0, hasRead));
}
fis.close();
return sb.toString();
}
/**
* 根据文件路径读取byte[] 数组
*/
public static byte[] readFileByBytes(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException(filePath);
} else {
ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(file));
short bufSize = 1024;
byte[] buffer = new byte[bufSize];
int len1;
while (-1 != (len1 = in.read(buffer, 0, bufSize))) {
bos.write(buffer, 0, len1);
}
byte[] var7 = bos.toByteArray();
return var7;
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException var14) {
var14.printStackTrace();
}
bos.close();
}
}
}
/**
* 保存文件
* @param path
* @param fileName
* @param str
* @return
*/
public static boolean saveFile(String path,String fileName,String str){
boolean flag = true;
if(!StringUtils.isEmpty(path) && !StringUtils.isEmpty(fileName) && !StringUtils.isEmpty(str)){
createFileByGBK(path, fileName, str);
LogUtils.logInfo("---------文件保存成功--------");
}else{
flag = false;
LogUtils.logError("--------文件保存失败--------");
}
return flag;
}
/**
*
* @param path
* @param fileName
* @param str
* @return
*/
public static boolean saveFile2(String path,String fileName,String str){
boolean flag = true;
if(str != null && !str.equals("")){
String dateDir = new SimpleDateFormat("yyyy/MM/dd/").format(new Date());
path = path + "/" + dateDir + "/";
try {
createFileByGBK(path,fileName,str);
} catch (Exception e) {
flag = false;
LogUtils.logInfo("------- 文件保存失败-----------"+e);
}
}else{
flag = false;
LogUtils.logError("------- 文件信息内容为空-----------");
}
return flag;
}
/**
* 以GBK编码存储文件
*2018年5月7日
@param path
@param fileName
@param str
*/
private static void createFileByGBK(String path, String fileName,String str) {
StringBuffer sb = new StringBuffer(str);
try {
createFile(path,sb, "GBK",fileName);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 写文件到指定目录
@param path
@param str
@param encode
@param fileName
*/
public static synchronized void createFile(String path, StringBuffer str, String encode,String fileName) {
boolean type = createDir(path);
if(type == true ){
BufferedWriter writer = null;
try {
FileOutputStream writerStream = new FileOutputStream(path+ "/" + fileName);
writer = new BufferedWriter(new OutputStreamWriter(writerStream,encode));
writer.write(str.toString());
LogUtils.logInfo("------------文件写入完成--------------");
} catch (Exception e) {
LogUtils.logError("-------------文件写入失败-------------");
e.printStackTrace();
}finally{
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}else{
LogUtils.logInfo("-------------文件路径不存在--------------path:"+path+"---fileName:"+fileName+".xml");
}
}
/**
* 根据传入path,创建目录
*2018年5月7日
@param path
@return
*/
private static boolean createDir(String path) {
File dir = new File(path);
if(dir.exists()) {
LogUtils.logInfo("--------文件夹已经存在---------:");
return true;
}else{
if(!path.endsWith(File.separator))
path = path + File.separator;
// 创建单个目录
if(dir.mkdirs()) {
LogUtils.logInfo("创建目录:" + path + " 成功!");
return true;
} else {
LogUtils.logInfo("创建目录:" + path + " 失败!");
return false;
}
}
}
public static void CopySingleFileTo(String oldPathFile, String targetPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPathFile);
String targetfile = targetPath ;
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPathFile); //读入原文件
FileOutputStream fs = new FileOutputStream(targetfile);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
//System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}