欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

读取资源properties文件的写法

程序员文章站 2022-05-10 08:46:23
...
读取资源文件的方法有很多种,这种打jar是最方便的不会再报找不到文件了


import java.io.InputStream;
import java.util.Properties;

public class ReadFile {

public static void main(String[] args) throws Exception {
//相对路径
// String fileName = "../../file.txt";
String fileName = "../../file.properties";
ReadFile rf = new ReadFile();
rf.readProp(fileName);
}

public void readText(String fileName) throws Exception {
InputStream is = this.getClass().getResourceAsStream(fileName);
byte[] b = new byte[1024];
is.read(b);
System.out.println(new String(b).trim());
}

public void readProp(String fileName) throws Exception {

//读取文件
InputStream is = this.getClass().getResourceAsStream(fileName);
Properties prop = new Properties();
prop.load(is);
System.out.println("key: " + prop.getProperty("key"));
}

}


相关标签: Properties