用途:将生成的文本内容TXT格式保存在服务器上面
获取Tomcat的路径
String path = System.getProperty("catalina.home");
filePath:路径+文件名
content:保存的内容
public static void createFile(String filePath,String content){
BufferedWriter output = null;String[] split = filePath.split("\\\\");
try {
File f = null;
String path = split[0];
for (int i = 0; i < split.length -1; i++) {
path = path+ "/" + split[i+1];
f = new File(path);
if (!f.getParentFile().exists()) { //判断是否改文件夹是否存在
f.getParentFile().mkdir();// 不存在则创建
}
}
File f1 = new File(filePath);
f.createNewFile();
output = new BufferedWriter(new FileWriter(f1));
output.write(content);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}