//文件名,要写入的文本
public void writeToFile(String filename,String text) {
String path = CjfwzAction.class.getClassLoader().getResource("/").getPath();
path = path.replace("/WEB-INF/classes", "");
String logUrl = path+"js";
// String logUrl=ServletActionContext.getRequest().getSession().getServletContext().getRealPath("/js");
File f=new File(logUrl);
if (!f.exists()) {
f.mkdirs();
}
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(logUrl+"/"+filename)), "utf-8"));
bufferedWriter.write(text);
bufferedWriter.newLine();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
//Close the BufferedWriter
try {
if (bufferedWriter != null) {
bufferedWriter.flush();
bufferedWriter.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
读文件:
public void readJsonArea(){
long start = System.currentTimeMillis();
try {
String path = FrontCache.class.getResource("/").getPath();
path = path.replace("%20", " ");
File file = new File(path + "__area.json");
List<String> list = FileUtils.readLines(file, "utf-8");
SystemManager.areaMap = JSON.parseObject(list.get(0),new TypeReference<Map<String,Area>>(){});
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeToFile(String filename,String text) {
String path = CjfwzAction.class.getClassLoader().getResource("/").getPath();
path = path.replace("/WEB-INF/classes", "");
String logUrl = path+"js";
// String logUrl=ServletActionContext.getRequest().getSession().getServletContext().getRealPath("/js");
File f=new File(logUrl);
if (!f.exists()) {
f.mkdirs();
}
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(logUrl+"/"+filename)), "utf-8"));
bufferedWriter.write(text);
bufferedWriter.newLine();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
//Close the BufferedWriter
try {
if (bufferedWriter != null) {
bufferedWriter.flush();
bufferedWriter.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
读文件:
public void readJsonArea(){
long start = System.currentTimeMillis();
try {
String path = FrontCache.class.getResource("/").getPath();
path = path.replace("%20", " ");
File file = new File(path + "__area.json");
List<String> list = FileUtils.readLines(file, "utf-8");
SystemManager.areaMap = JSON.parseObject(list.get(0),new TypeReference<Map<String,Area>>(){});
} catch (IOException e) {
e.printStackTrace();
}
}