import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import com.retail.supmarket.http.controller.SweepCodePaymentController;
public class ReadFileUtil {
/**
*
* @Title: readFile 从properties文件中读取url的配置信息
* @Description: TODO String
* @author Administrator
*/
public static String readFile(String proper) {
Properties properties = new Properties();
// 使用ClassLoader加载properties配置文件生成对应的输入流
InputStream in = SweepCodePaymentController.class.getClassLoader().getResourceAsStream("url.properties");
// 使用properties对象加载输入流
try {
properties.load(in);
} catch (IOException e) {
System.out.println("请在配置中检查url的配置是否正确" + e);
e.printStackTrace();
}
// 获取key对应的value值
String getHttpProperty = properties.getProperty(proper);
return getHttpProperty;
}
}
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
public class ReadFileUtil {
private static final String DEFAULT_ENCODING = "UTF-8";
/**
*
* @Title: readFile 从properties文件中读取url的配置信息
* @Description: TODO String
* @author Administrator
*/
public static String readFile(String proper) {
try {
// 获取文件流(方法1或2均可)
//InputStream inputStream = new BufferedInputStream(
//new FileInputStream(new File("/src/main/resources/url.properties"))); // 方法1
InputStream inputStream =
Thread.currentThread().getContextClassLoader().getResourceAsStream("url.properties");
// //方法2
Properties prop = new Properties();
prop.load(new InputStreamReader(inputStream, DEFAULT_ENCODING)); // 加载格式化后的流
String driverClassName = prop.getProperty(proper);
return driverClassName;
} catch (FileNotFoundException e) {
System.out.println("properties文件路径有误!");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return proper;
}
}
java读取资源参考博客:
https://www.cnblogs.com/shuimuzhushui/p/7247864.html
在jar文件下面读取:
https://www.cnblogs.com/TonyYPZhang/p/6298422.html
用类加载读取配置文件:
https://www.cnblogs.com/1540340840qls/p/6184109.html