项目中多用此种方法
InputStream in = MqttUtil.class.getClassLoader().getResourceAsStream("application.properties");
Properties properties = new Properties();
此处是读取相对路径的文本,绝对路径读取
import org.springframework.util.ResourceUtils;
import java.io.*;
public class Application {
public static void main(String[] args) throws IOException {
InputStream in = new FileInputStream(ResourceUtils.getFile("classpath:test.json"));
read(in);
read();
}
public static void read (InputStream resourceAsStream)throws IOException{
String readData=null;
StringBuffer reString=new StringBuffer() ;
InputStreamReader inputStreamReader = new InputStreamReader(resourceAsStream, "UTF-8");
BufferedReader br = new BufferedReader(inputStreamReader);
while((readData = br.readLine())!=null){
reString.append(readData);
}
br.close();
System.out.println(reString.toString());
}
public static Properties read ()throws IOException {
InputStream in = new FileInputStream(ResourceUtils.getFile("classpath:test.json"));
Properties properties = new Properties();
properties.load(in);
return properties;
}
}