|
src
|
t090417
|
test.properties
Read.java
test.properties:
TEST=test
Read.java:
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.IOException
import java.util.Properties
public class Read {
public static String TEST
private static Properties loadPropertyFile() throws FileNotFoundException,IOException{
Properties properties = new Properties()
FileInputStream fs = new FileInputStream("src/t090417/test.properties")
properties.load(fs)
return properties
}
public static void loadProperty(){
try{
Properties properties = loadPropertyFile()
TEST = properties.getProperty("TEST")
System.out.println("read from properties: "+TEST)
}catch(Exception e){
e.printStackTrace()
}
}
public static void main(String[] args) {
loadProperty()
}
}
其中用的就是相对路径!
一个例子,如果有一个Test文件夹里面有test.java和hello.txt.如果你想用test.java操作hello.txt只要在test.java中这样写File file=new File("hello.txt")//这样就是相对路径。如果文件结构是
Test文件夹
... |------test.java
... |------hello.txt
... |------source文件夹
................. |---------world.txt
如果想在test.java中操作world.txt。只要这样写File file=new File("source/world.txt")
另外,在web开发中/代表项目文件夹根目录,当然也有可能代替webapps,区分方法是:如果/开头的uri是给浏览器解析则/代表webapps,如果是给服务器后台解析,则代表项目文件
servlet里面通过String basePath=session.getServletContext().getRealPath("/")获取webRoot目录真实路径。比如d:\tomcat 6\webapps\项目名
webRoot下面的resources目录通过File path = new File(basePath, "resources")获取
其它同理
如果是jsp的话,获取resources目录直接通过“<%=request.getContextPath()%>/resources”获取