I am having enormous difficulty writing to a simple text file in Java. Every search I’ve done for the IO exception I’m getting is turning up plenty of suggestions, but none of them apply to this situation.
Here is my project structure:
MyEclipseProject/
src/
com.myprogram.utils
MyProgram
bin/
And my code for MyProgram.java:
package com.myprogram.utils;
import java.io.FileWriter;
public class MyProgram
{
public static void main(String[] args)
{
FileWriter oWriter = new FileWriter(new File("logs/system.log"));
oWriter.write("This never gets logged because JRE can't find the file");
}
}
The exception message I’m getting states: logs\system.log (The system cannot find the path specified).
The first time I tried, I did so without having first created the logs/ directory and its subsequent log file. My understanding was that, if Java couldn’t find the file, it would create it for you.
I have now placed a logs folder – with a blank system.log file – inside: (1) my project root (MyEclipseProject), (2) the src/ folder, (3) the src/com.myprogram.utils package, and (4) the bin folder, and I am getting the same exact error. I’m wondering: could I have an OS/persmissions thing going on? Could my app be trying to create the folder and log file but be getting denied permission to do so by Windows 7? If so, what do I do?!?!
If not, where in tarnation do I place logs/system.log???
Thank you for any clarity to this!
To see which directory you are in, add the following print statement:
This will tell you where you should create the
logsdirectory.