java中 读取文件时想用相对路径,代码怎么写?
1个回答
推荐于2017-05-25
展开全部
test
|
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();
}
}
其中用的就是相对路径!
|
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();
}
}
其中用的就是相对路径!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询