FilesOperation.java
1010 Bytes
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
package com.sitech.ismp.coll.busi.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
public class FilesOperation {
/**
* 创建业务采集脚本
* @throws Exception
* @throws
* @since Ver 1.1
*/
public static synchronized File createFiles(String filename, String context)
throws Exception {
File file = null;
BufferedWriter bw = null;
try {
// 文件所在目录
File redirect = null;
if (filename.lastIndexOf("/") != -1) {
String dir = filename.substring(0, filename.lastIndexOf("/"));
redirect = new File(dir);
if (!redirect.exists()) {
// 目录不存在则创建
redirect.mkdirs();
}
}
file = new File(filename);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file);
bw = new BufferedWriter(fw);
bw.write(context);
} catch (Exception e) {
throw e;
} finally {
if (bw != null) {
bw.flush();
bw.close();
}
}
return file;
}
}