FileOutputStream os=new FileOutputStream("d:/myFinalProperties");
你可以指定绝对路径,这样放在那里都可以了

解决方案 »

  1.   

    FileOutputStream os=new FileOutputStream("myFinalProperties");
    这样写就可以和class文件放在统一目录
      

  2.   

    java.io里面的所有关于file的东西,都是从project根目录读文件的。所以,比如用Jbuilder,应该放在同src,classes folder并列的地方。但是你的写法由于没有给出文件的extention name,那么myDefaultProperties也不可以有ext name.建议改成"myDefault.properties".
    ClassLoader instance.getResource("...")是从class loader根目录读文件,所以这种情况文件应该放在classes folder下面。java.util.ResourceBundle就是这样读文件的。
      

  3.   

    ***Using File, as well as FileInputStream, FileOutputStream, FileReader, and FileWriter.
    All above Classes are under the J2SE io package, and they appear to have the same atitude when finding the file. They are looking to the project loader directory to find the file. The following table shows the root folder of the project when the project is start in different environment:
    Environment          Root Folder
    JBuilder                   The Project Root Folder
    bat file to start a jar The Batch File Folder
    Tomcat                   Tomcat Root Folder
    Resin                   Resin Root Folder
    The Log4J will generate log files under this folder; the Velocity will look for the template file under this folder when velocity property is set as default.
    When the file path/name passed to create a new File instance starts with a slash ‘/’, the JVM understands it as to locate the file from the driver root directory, such as C:\ or D:\. Regardless the environment the project starts from, it goes to the top root directory of the environment.
    Now, let’s look at an alternative way of retrieving properties other than using ResourceBundle. As one should have noticed that the ResourceBundle eliminates the property file to have properties extension name, and moreover, it eliminates the properties file(s) to be located relative to the classes. In such a case if one would rather have the property file in the directory of the project root other than compress it into the jar file, or if one would rather call the property file in other extension name, the ResourceBundle cannot handle that. If the internationalization is not a major concern, (even though it can still be satisfied by user implementation) the following sample shows how to save properties and how to retrieve properties using java.util.Properties class.
            Properties p = new Properties();
            p.setProperty("dummy-key", "dummy-value");
            FileOutputStream fos = new FileOutputStream("FileName.Extention");
            p.store(fos, "Title");
            fos.close();
    ///////////////////////////////////////////////////////////////////////
            Properties p = new Properties();
            FileInputStream fis = new FileInputStream("FileName.Extention");
            p.load(fis);
            fis.close();
            String dummy-key = p.getProperty("dummy-key", "default-value");
    The file will be saved to and/or retrieved from the project root folder as appeared in the above table.
    ***Retrieving files under class folder or in the jar.
    If new File(..) is only able to locate the files relative to the project loader, the ClassLoader’s getResource method will get files under class folder or inside the jar. The following code shows how to get the file located under class folder or inside a jar and create a File instance:
    File file = new File(Thread.currentThread().getContextClassLoader().getResource("test.doc").getFile());
    Alternatively, we can use the following code:
    File file = new File(AnyClass.class.getClassLoader().getResource(“test.doc”).getFile());
    In either way, it will search the first level class loader, which will be the class folder when running in JBuilder, Tomcat, or Resin, or the project jar file to locate the desired file. If the file is not found, it will than go to library jars to locate the file. In other words, the project level files is able to overwrite the library level files as long as they have the exact name and relative path respect to the class root folder.