关于一个文件存放路径的问题?

koko214 2003-06-26 12:15:56
下面是一个关于处理程序属性,包括系统属性和程序自定义属性的小程序,
import java.io.*;
import java.util.Properties;

public class ManageProp
{
public static void main(String args[]) throws IOException
{
Properties p=new Properties(System.getProperties());
FileInputStream is=new FileInputStream("myDefaultProperties");
p.load(is);
p.list(System.out);
p.put("waitForSet","setIt");
FileOutputStream os=new FileOutputStream("myFinalProperties");
p.save(os,"final properties");
}
}

其中文件myDefaultProperties的内容为:
prog.name=ManageProp
prog.func=Demo management of properties
waitForSet=123


请问这个文件应该放在什么位置才可以使程序正确执行啊?
我是一位初学者,这个程序是从书上看来的,有些地方不是很懂,希望各位高手指教`~

...全文
64 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hayai 2003-06-26
  • 打赏
  • 举报
回复
***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.
hayai 2003-06-26
  • 打赏
  • 举报
回复
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就是这样读文件的。
long_zhi 2003-06-26
  • 打赏
  • 举报
回复
是同一,不是统一
long_zhi 2003-06-26
  • 打赏
  • 举报
回复
FileOutputStream os=new FileOutputStream("myFinalProperties");
这样写就可以和class文件放在统一目录
moumouren 2003-06-26
  • 打赏
  • 举报
回复
FileOutputStream os=new FileOutputStream("d:/myFinalProperties");
你可以指定绝对路径,这样放在那里都可以了

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧