Question

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!

Was it helpful?

Solution

To see which directory you are in, add the following print statement:

System.out.println(new File(".").getAbsolutePath());

This will tell you where you should create the logs directory.

OTHER TIPS

try BufferedWriter

public static void main(String[] args {

String filename = "C:\\mynewtextfile.txt";
FileWriter fstream;
   try {
   fstream = new FileWriter(filename);
   BufferedWriter out = new BufferedWriter(fstream);
            out.write("My Name is Bobby");
            out.newLine();
            out.write("This is my text file.");                       
            out.flush();
            out.close();
        } 
   catch (IOException e) {
            e.getClass();
        }

}

Path you are using is relative to current working directory. Your current working directory depends on how are you running your application. I guess it is MyEclipseProject. So, you have to create directory log under your project (at the same level where src and bin directories are) and then create file system.log into this directory. Then try again. I believe your code will start working.

It it does not try to print new File("logs/system.log").getAbsolutePath() before writing to file. It will show you where the system is expecting to see your file.

When creating a file path on modern filesystems (at least, this is true for Windows, Mac, and the flavors of Linux that I am familiar with), there are absolute paths and there are relative paths. An absolute path is the path through the filesystem down to the specific file you are looking for, such as C:\Program Files\Application\application.exe. Relative paths are (as implied) relative to the current location.

If I run main() with the line 'System.out.println(new File(".").getAbsolutePath());', for example, I get the output

C:\my-workspace\myproject\.

The . represents the current directory. That's why you can use that to find where the directory should be created.

You are correct that Java will create the file if it can't find it, but the same is not true of the directory. You have to create the directory either prior to running the code or in code itself using the File#mkdir() method.

depends how you call the application, but you should create logs directory inside project root and you should be able to write a file in there.

Java won't by itself create this directory during opening a stream.

You can have problem with permissions. Try:

System.out.println("can write: " + new File(./logs).canWrite());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top