0
我有以下java代码,它从资源文件夹中读取config.properties
。从资源文件夹读取conf文件时出现意外的结果
import java.io.InputStream;
import java.util.Properties;
public class TestProperties {
static ClassLoader classloader = Thread.currentThread().getContextClassLoader();
static InputStream input = classloader.getResourceAsStream("config.properties");
public static void main(String[] args) throws InterruptedException {
while (true) {
new TestProperties().readPropertiesFile();
Thread.sleep(2000);
}
}
private void readPropertiesFile() {
Properties properties = new Properties();
try {
properties.load(input);
int threads = Integer.parseInt(properties.getProperty("num_of_workers"));
System.out.println("num_of_workers: " + threads);
} catch (Exception e) {
System.out.println("hey something went wrong: " + e.getMessage());
}
}
}
结果:
- num_of_workers:2
- 哎出事了:空
- 哎出事了:空
- 哎出事了:空
它给我重新(num_of_workers: 2
)第一次审查,但在此之后,它给出空。
但是当我改变
static ClassLoader classloader
到ClassLoader classloader
static InputStream input
到InputStream input
我得到预期的结果:
- num_of_workers:2
- 个num_of_workers:2个
- num_of_workers:2个
- num_of_workers:2
能否请你解释这种现象?
消耗?它是否像队列一样工作(即在使用后它会被删除) –
不是你在第一次迭代中使用它的队列。一旦你使用它,你不能再简单地使用它。你需要重新设置,如果你想读一遍 –
的,你需要将其标记为第一 –