I'm trying to write some test messages or some text content to file through java in ofbiz framework. If the file already has content, recent messages has to append at bottom of file.
In brief, I want a functionality like Debug.log in ofbiz. As it writes everything to debug.log file, I want a separate file to write my text messages.
I tried this,
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();
}
}
But it is throwing FileNotFoundException
.
As, I'm not expert in ofbiz, i'm not sure about the file path. If file path is the problem please suggest me solution.
You can use Logger to log message.
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) {
}
Dont Forget to mention the absolute file path. Ex: /root/apache-ofbiz/applications/party/webapp/partymgr/test.txt
Or
Get the relative path from the component
filepath = ComponentConfig.getRootLocation("party")+"webapp/log/test.txt";