Java 阅读json文件以及如何更改硬编码路径
问题:我想更改硬编码json文件路径。路径将是从细节开始的,但我不知道怎么做 这是我的主要节目Java 阅读json文件以及如何更改硬编码路径,java,json,Java,Json,问题:我想更改硬编码json文件路径。路径将是从细节开始的,但我不知道怎么做 这是我的主要节目 public class Program { // hard coding json file path private static final String filePath = "C:/appSession.json"; public static void main(String[] args) { taskManager();
public class Program {
// hard coding json file path
private static final String filePath = "C:/appSession.json";
public static void main(String[] args)
{
taskManager();
}
public static void taskManager()
{
detailsHM = jsonParser(filePath);
}
public static HashMap<String, String> jsonParser(String jsonFilePath)
{
HashMap<String, String> detailsHM = new HashMap<String, String>();
String refGene = "";
try {
// read the json file
FileReader reader = new FileReader(filePath);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
}
公共类程序{
//硬编码json文件路径
私有静态最终字符串filePath=“C:/appSession.json”;
公共静态void main(字符串[]args)
{
taskManager();
}
公共静态void taskManager()
{
detailsHM=jsonParser(filePath);
}
公共静态HashMap jsonParser(字符串jsonFilePath)
{
HashMap detailsHM=新的HashMap();
字符串refGene=“”;
试一试{
//读取json文件
FileReader=新的FileReader(filePath);
}捕获(FileNotFoundException ex){
例如printStackTrace();
}
}
}
下面是另一个名为CustomConfiguration的类
public class CustomConfiguration {
private static HashMap<String, String> detailsListHM =new HashMap<String,String>();
public static void readConfig(String a) {
//read from config.properties file
try {
String result = "";
Properties properties = new Properties();
String propFileName = a;
InputStream inputStream = new FileInputStream(propFileName);
properties.load(inputStream);
// get the property value and print it out
String lofreqPath = properties.getProperty("lofreqPath");
String bamFilePath = properties.getProperty("bamFilePath");
String bamFilePath2 = properties.getProperty("bamFilePath2");
String resultPath = properties.getProperty("resultPath");
String refGenPath = properties.getProperty("refGenPath");
String filePath = properties.getProperty("filePath");
Set keySet = properties.keySet();
List keyList = new ArrayList(keySet);
Collections.sort(keyList);
Iterator itr = keyList.iterator();
while (itr.hasNext()) {
String key = (String) itr.next();
String value = properties.getProperty(key.toString());
detailsListHM.put(key, value);
}
} catch (IOException ex) {
System.err.println("CustomConfiguration - readConfig():" + ex.getMessage());
}
}
public static HashMap<String, String> getConfigHM() {
return detailsListHM;
}
公共类自定义配置{
私有静态HashMap detailsListHM=新HashMap();
公共静态void readConfig(字符串a){
//从config.properties文件读取
试一试{
字符串结果=”;
属性=新属性();
字符串propFileName=a;
InputStream InputStream=新文件InputStream(propFileName);
属性。加载(inputStream);
//获取属性值并将其打印出来
字符串lofreqPath=properties.getProperty(“lofreqPath”);
字符串bamFilePath=properties.getProperty(“bamFilePath”);
字符串bamFilePath2=properties.getProperty(“bamFilePath2”);
字符串resultPath=properties.getProperty(“resultPath”);
字符串refGenPath=properties.getProperty(“refGenPath”);
字符串filePath=properties.getProperty(“filePath”);
Set keySet=properties.keySet();
List keyList=新数组列表(键集);
集合。排序(键列表);
迭代器itr=keyList.Iterator();
while(itr.hasNext()){
字符串键=(字符串)itr.next();
字符串值=properties.getProperty(key.toString());
详细列表输入(键、值);
}
}捕获(IOEX异常){
System.err.println(“CustomConfiguration-readConfig():”+ex.getMessage());
}
}
公共静态HashMap getConfigHM(){
返回详情列表;
}
添加一个新的属性调用“json filepath”,如下所示
String filePath = properties.getProperty("json-filepath");
因此,最终用户甚至可以在运行时更改json文件路径。您可以使用主参数传递filePath参数
public static void main(String[] args) {
String filePath = null;
if(args.length > 0) {
filePath = args[0];
}
}
然后像这样调用主类:
java Program C:/appSession.json
这段代码执行吗?我想你的
程序
类需要一个私有静态HashMap detailsHM=null;
而且我不知道你在程序
类中在哪里使用自定义配置
对象?