Writing to a file, where is the output file?

        FileWriter outFile = null;
        try {
            outFile = new FileWriter("member.txt");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
out.println("test");

Running this command, where is member.txt located? I am using Windows Vista. UAC is enabled, so when I run it, I don’t think it writes to a txt file. The txt file was created, but it is empty.

+3
source share
5 answers

Relative paths in Java IO relate to the current working directory. In Eclipse, this is usually the root of the project. You also write outinstead outFile. Here the younger rewrite:

    File file = new File("member.txt");
    FileWriter writer = null;
    try {
        writer = new FileWriter(file);
        writer.write("test");
    } catch (IOException e) {
        e.printStackTrace(); // I'd rather declare method with throws IOException and omit this catch.
    } finally {
        if (writer != null) try { writer.close(); } catch (IOException ignore) {}
    }
    System.out.printf("File is located at %s%n", file.getAbsolutePath());

Closing is mandatory because it flushes the recorded data to a file and releases the file lock.

, Java IO. , classpath. ClassLoader#getResource(), getResourceAsStream() ..

+6

( ), .

+4

Java, , " → ..."

"". " ". , Java-.

"member.txt" , , " ".

+2

IDE. , .java. , , Eclipse Netbeans, .

0

Eclipse ( , ). Project Explorer, ( F5).

" " "".

0

Source: https://habr.com/ru/post/1760384/


All Articles