gpt4 book ai didi

java - 从添加到项目的 jar 中读取动态 Web 项目中存在的属性文件

转载 作者:太空宇宙 更新时间:2023-11-04 06:20:48 25 4
gpt4 key购买 nike

我正在开发一个动态 Web 项目(DWP),它有一个属性文件。已创建一个 jar,该 jar 应该读取 DWP 中属性文件的内容以及属性文件中的更改,并将更改合并到 DWP 中。我无法从 jar 中读取属性文件。我想知道DWP中的jar如何读取DWP的属性文件

这是 jar 的代码

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

public class fileReader extends Thread
{
publicstatic long oldTMS;
long currentTMS;
File file;
public static long limit;
//static String Path = "./configuration/RequestLimit.properties";
static String Path="configuration/RequestLimit.properties";
public static Properties properties;
public fileReader() throws IOException, InterruptedException {
System.out.println("Reading the property file for the first time");
Thread thread = new Thread();
setDaemon(true);
FileInputStream in = new FileInputStream(Path);
System.out.println("fd:"+in.getFD());
properties = new Properties();
properties.load(in);
String requestLimit = properties.getProperty("RequestLimit");
System.out
.println("request limit as read from the property file is ----"
+ requestLimit);
limit = Long.parseLong(properties.getProperty("RequestLimit"));
System.out.println("request limit as per the reading is ---" + limit);
start();
// waits for the thread to die
join();
}
public void run() {
System.out.println(" thread starts");
if (Thread.currentThread().isDaemon()) {
while (true) {
try {
Thread.sleep(3000);
System.out.println("entered in infinite loop");
file = new File(Path);
System.out.println("file name is --------" + file);
currentTMS = file.lastModified();
SimpleDateFormat dateFormat = new SimpleDateFormat(
"E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println("file initial change time stamp:::::"
+ dateFormat.format(currentTMS));
try {
System.out
.println("lets see if the file has changed or not !!");
FileInputStream d = new FileInputStream(file);
if (oldTMS != currentTMS) {
System.out.println("checking for old and new time stamp!");
oldTMS = currentTMS;
System.out.println("file modified last at-----------"
+ oldTMS);
// reload property
try {
System.out.println("Reloading property file");
properties.load(d);
System.out.println("After Reloading property file properties : "
+ properties
.getProperty("RequestLimit"));
limit = Long.parseLong(properties
.getProperty("RequestLimit"));
System.out.println("request limit as per the reading is ---"
+ limit);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
catch (InterruptedException e2)
{
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}
}
}

像这样在 DWP 中使用 jar

package com.ING;    
import java.io.IOException;
import com.change.fileReader;
public class deamonCaller
{
public long callDeamon() throws IOException, InterruptedException
{
fileReader fr = new fileReader();
Long requestLimit =fileReader.limit;
return requestLimit;
}
}

最佳答案

请尝试使用以下代码。希望对您有很大帮助。

static String Path="configuration/RequestLimit.properties";
loadProperties(Path);

private void loadProperties(String propertiesName) {

if (properties != null) {
return;
}

// Properties properties = null;
InputStream inputStream = null;

inputStream = this.getClass().getResourceAsStream(propertiesName);
if (inputStream == null) {
throw new Exception(propertiesName + "something");
}

properties = new Properties();
try {
properties.load(inputStream);
} catch (IOException e) {
throw new Exception(e);
}
}

关于java - 从添加到项目的 jar 中读取动态 Web 项目中存在的属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27442308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com