public static String read(String fileName,String key) {
FileInputStream inputFile = null;
String filePath = null;
Properties pro = new Properties();
try {
filePath = new String(URLDecoder.decode(PropertyUtil.class
.getClassLoader().getResource(fileName).getPath(),
"UTF-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("-----------"+filePath);
try {
String value = "";
inputFile = new FileInputStream(filePath);
pro.load(inputFile);
inputFile.close();
if(pro.contains(key)){
value = pro.getProperty(key);
return value;
}else{
return "";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return "";
} catch (IOException e) {
e.printStackTrace();
return "";
} catch (Exception e) {
e.printStackTrace();
return "";
}
}求大虾们帮忙看看那
FileInputStream inputFile = null;
String filePath = null;
Properties pro = new Properties();
try {
filePath = new String(URLDecoder.decode(PropertyUtil.class
.getClassLoader().getResource(fileName).getPath(),
"UTF-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("-----------"+filePath);
try {
String value = "";
inputFile = new FileInputStream(filePath);
pro.load(inputFile);
inputFile.close();
if(pro.contains(key)){
value = pro.getProperty(key);
return value;
}else{
return "";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return "";
} catch (IOException e) {
e.printStackTrace();
return "";
} catch (Exception e) {
e.printStackTrace();
return "";
}
}求大虾们帮忙看看那
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;public class PropertyUtil { public static void main(String[] args){
System.out.println(read("aa.properties","aa"));
}
public static String read(String fileName, String key) {
InputStream inputFile = null;
String filePath = null;
Properties pro = new Properties();
try {
filePath = PropertyUtil.class.getResource(fileName).getPath();
System.out.println("-----------" + filePath);
inputFile = new FileInputStream(new File(PropertyUtil.class.getResource(fileName).getPath()));
} catch (Exception e1) {
e1.printStackTrace();
}
try {
String value = "";
pro.load(inputFile);
inputFile.close();
if (pro.containsKey(key)) {
value = pro.getProperty(key);
return value;
} else {
return "";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return "";
} catch (IOException e) {
e.printStackTrace();
return "";
} catch (Exception e) {
e.printStackTrace();
return "";
}
}}
filePath = new String(URLDecoder.decode(PropertyUtil.class.getClassLoader().getResource(fileName).getPath(),"UTF-8"));的前面加一句:
System.out.println(fileName.getAbsolutePath());看看fileName的绝对路径对不对 如果不对 照着改就行了
System.out.println(new File(fileName).getAbsolutePath());
String ipLibPath = bundle.getString("adms.iplib.path");
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;public class PropertyUtil { public static void main(String[] args){
System.out.println(read("aa.properties","aa"));
}
public static String read(String fileName, String key) {
InputStream inputFile = null;
String filePath = null;
Properties pro = new Properties();
try {
filePath = PropertyUtil.class.getResource(fileName).getPath();
System.out.println("-----------" + filePath);
inputFile = new FileInputStream(new File(PropertyUtil.class.getResource(fileName).getPath()));
} catch (Exception e1) {
e1.printStackTrace();
}
try {
String value = "";
pro.load(inputFile);
inputFile.close();
if (pro.containsKey(key)) {
value = pro.getProperty(key);
return value;
} else {
return "";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return "";
} catch (IOException e) {
e.printStackTrace();
return "";
} catch (Exception e) {
e.printStackTrace();
return "";
}
}}