Java 属性类错误

我有这段代码。


// On a thread

try {

    WatchService watcher = FileSystems.getDefault().newWatchService();

    Path directory = Paths.get("properties");

    WatchKey watchKey = directory.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);

    while (true) {

        for (WatchEvent<?> event : watchKey.pollEvents()) {

            Path changed = (Path) event.context();

            if (changed.toString().equals("radar.properties")) {

                System.out.println("read call:");

                readProperties();

            }


        }


        if (!watchKey.reset()) {

            break;

        }

    }

} catch (IOException e) {

    FCSLogger.LOGGER.log(Level.SEVERE, "Exception while setting up WatchService", e);

}


// Method called by the above code


private void readProperties() {

    try {

        InputStream input = new FileInputStream(Paths.get("properties", "radar.properties").toString());

        Properties prop = new Properties();

        prop.load(input);

        updateRate = Integer.parseInt(prop.getProperty("updateRate"));

        System.out.println(updateRate);

        input.close();

    } catch (IOException e) {

        e.printStackTrace();

    }

}

它在第一次调用时返回正确的结果,然后阻塞整个线程。我已经隔离了这个方法中的错误,因为当没有调用这个方法时,其他一切都完美无缺。我想知道我在这里做错了什么。控制台输出快照:


// First change of file:

read call:

10

read call:

10


// Second change of file:

read call:


// I keep changing but nothing happens:


临摹微笑
浏览 82回答 1
1回答

眼眸繁星

它可能是readPropertiesthrows NumberFormatException,并导致您的观察线程退出。您可以包装调用readProperties并捕获异常;这样至少观察者会在出现异常情况时继续观察。你应该使用take, 以便观察线程阻塞。您当前的解决方案导致 100% 的 CPU 使用率。请参阅下面修改后的代码。我添加了一个编写器线程来更新文件,并且(不出所料?)readProperties可能会失败,因为我们在写入文件时正在访问该文件。一个可能的输出是:....property=5000property=nulljava.lang.NumberFormatException: null&nbsp; at java.base/java.lang.Integer.parseInt(Integer.java:614)&nbsp; at java.base/java.lang.Integer.parseInt(Integer.java:770)&nbsp; at cl.ClientPoll.readProperties(ClientPoll.java:26)&nbsp; at cl.ClientPoll.run(ClientPoll.java:46)property=6000property=7000....因此在观看时,您可以: 跳过错误并继续;或使用其他API,使正在写入的文件在写入时被锁定。示例代码package cl;import java.io.File;import java.io.FileInputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.nio.file.FileSystems;import java.nio.file.Path;import java.nio.file.Paths;import java.nio.file.StandardWatchEventKinds;import java.nio.file.WatchEvent;import java.nio.file.WatchKey;import java.nio.file.WatchService;import java.util.Properties;public class ClientPoll extends Thread {&nbsp; private void readProperties() {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; Path path = Paths.get("radar.properties");&nbsp; &nbsp; &nbsp; InputStream input =new FileInputStream(path.toString());&nbsp; &nbsp; &nbsp; Properties prop = new Properties();&nbsp; &nbsp; &nbsp; prop.load(input);&nbsp; &nbsp; &nbsp; String property = prop.getProperty("updateRate");&nbsp; &nbsp; &nbsp; System.out.println("property="+property);&nbsp; &nbsp; &nbsp; int updateRate = Integer.parseInt(property);//&nbsp; &nbsp; &nbsp; System.out.println(updateRate);&nbsp; &nbsp; &nbsp; input.close();&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; e.printStackTrace(System.out);&nbsp; &nbsp; }&nbsp; }&nbsp; @Override&nbsp; public void run() {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; WatchService watcher = FileSystems.getDefault().newWatchService();&nbsp; &nbsp; &nbsp; Path directory = Paths.get(".");&nbsp; &nbsp; &nbsp; WatchKey watchKey = directory.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);&nbsp; &nbsp; &nbsp; while (true) {&nbsp; &nbsp; &nbsp; &nbsp; WatchKey wk = watcher.take();&nbsp; &nbsp; &nbsp; &nbsp; for (WatchEvent<?> event : wk.pollEvents()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Path changed = (Path) event.context();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (changed.toString().equals("radar.properties")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; readProperties();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace(System.out);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (!watchKey.reset()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; e.printStackTrace(System.out);&nbsp; &nbsp; } catch (InterruptedException e) {&nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }&nbsp; }&nbsp; public static void main(String[] args) {&nbsp; &nbsp; new ClientPoll().start();&nbsp; &nbsp; new Writer().start();&nbsp; }}class Writer extends Thread {&nbsp; @Override&nbsp; public void run() {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; for (int i=0;i<10;i++) {&nbsp; &nbsp; &nbsp; &nbsp; File f = new File("radar.properties");&nbsp; &nbsp; &nbsp; &nbsp; FileWriter fw = new FileWriter(f);&nbsp; &nbsp; &nbsp; &nbsp; fw.write("updateRate="+i*1000);&nbsp; &nbsp; &nbsp; &nbsp; fw.close();&nbsp; &nbsp; &nbsp; &nbsp; sleep(1000L);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; e.printStackTrace(System.out);&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println("exit");&nbsp; &nbsp; System.exit(0);&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java