Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

Why are Java property files not visible in C:Windows

I have a Java program "PropertyWrite" to write a property file "crazy.properties" into the directory C:Windows

import java.io.*;
import java.util.Properties;

public class PropertyWrite {

    public static void main(String[] args) throws Exception {

        File myprop = new File("C:\WINDOWS\crazy.properties");

        Properties props = new Properties();
        props.setProperty("testprop", "crazy");
        OutputStream os = new FileOutputStream(myprop);
        props.store(os, "comment");
        os.close();

        System.out.println(myprop.exists()); // returns true
    }
}

When i search for that file via Windows File Explorer, it is not visible
Also "Cmd -> dir" does not show the file
Somehow the file seems to be hidden

If i try to read the file with my program "PropertyRead"
it works fine and it prints the value "crazy" as expected

import java.io.*;
import java.util.Properties;

public class PropertyRead {

    public static void main(String[] args) throws Exception {

        File myprop = new File("C:\WINDOWS\crazy.properties");
        System.out.println(myprop.exists()); // returns true

        Properties props = new Properties();
        FileInputStream in = new FileInputStream(myprop);
        props.load(in);
        in.close();

        System.out.println(props.getProperty("testprop")); // prints crazy
    }
}

And now it's going crazy:
If i manually create the same file (crazy.properties with content -> testprop=handmade)
and move it to the folder C:Windows the Explorer is able to show the file.
The file seems visible

Now i restart "PropertyRead" -> it prints NOT "handmade" -> it prints "crazy"

What the hell???

Now i delete the file "C:WINDOWScrazy.properties" via Explorer and restart PropertyRead
the file is still there (but invisible) and it prints "crazy"

How can that be?

thanks in advance

question from:https://stackoverflow.com/questions/65834889/why-are-java-property-files-not-visible-in-c-windows

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Normal programs without admin rights are not allowed to write to e.g. the Windows directory.

For compatiblity reasons files written to such location may end up in a "VirtualStore" specific to your user profile in some windows versions.

Check %UserProfile%AppDataLocalVirtualStore if your file is there.

Edit: Some further infos how VirtualStore works can be found here: https://answers.microsoft.com/en-us/windows/forum/windows_7-windows_programs/please-explain-virtualstore-for-non-experts/d8912f80-b275-48d7-9ff3-9e9878954227


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...