我正试图在ofbiz框架中通过java编写一些测试消息或一些文本内容。如果文件已有内容,则最近的消息必须附加在文件的底部。
简而言之,我想要一个像ofbiz中的Debug.log这样的功能。当它将所有内容写入debug.log文件时,我想要一个单独的文件来编写我的短信。
我试过了,
File file = new File("/applications/party/webapp/partymgr/test.txt");
if (!file.exists()) {
try {
file.createNewFile();
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
Debug.logInfo("writing...........", module);
bw.write("this is first line in file");
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
但它正在抛出FileNotFoundException
因为,我不是ofbiz的专家,我不确定文件路径。如果文件路径有问题请建议我解决。
答案 0 :(得分:0)
您可以使用Logger记录消息。
Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
logger.setLevel(Level.INFO);
try {
FileHandler fileTxt = new FileHandler("/root/apache-ofbiz/applications/party/webapp/partymgr/test.txt",true);
SimpleFormatter formatterTxt = new SimpleFormatter();
fileTxt.setFormatter(formatterTxt);
logger.addHandler(fileTxt);
logger.log(Level.INFO,"this is first line in file");
} catch (SecurityException e) {
} catch (IOException e) {
}
不要忘记提及绝对文件路径。例如:/root/apache-ofbiz/applications/party/webapp/partymgr/test.txt
或
获取组件的相对路径
filepath = ComponentConfig.getRootLocation("party")+"webapp/log/test.txt";