Java属性类错误

Java属性类错误,java,Java,我有这段代码 // On a thread try { WatchService watcher = FileSystems.getDefault().newWatchService(); Path directory = Paths.get("properties"); WatchKey watchKey = directory.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY); while (tru

我有这段代码

// 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();
    }
}

可能是
readProperties
抛出
NumberFormatException
,导致您的观察线程退出

您可以包装对
readProperties
的调用并捕获异常;这样,在异常情况下,至少观察者会继续观察

您应该使用
take
,以便观察线程阻塞。您当前的解决方案会导致100%的cpu使用率

请参见下面修改的代码。我添加了一个writer线程来更新文件,并且(毫不奇怪?
readProperties
可能会失败,因为我们在写入文件时正在访问该文件。可能的输出是:

....
property=5000
property=null
java.lang.NumberFormatException: null
  at java.base/java.lang.Integer.parseInt(Integer.java:614)
  at java.base/java.lang.Integer.parseInt(Integer.java:770)
  at cl.ClientPoll.readProperties(ClientPoll.java:26)
  at cl.ClientPoll.run(ClientPoll.java:46)
property=6000
property=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 {
  private void readProperties() {
    try {
      Path path = Paths.get("radar.properties");
      InputStream input =new FileInputStream(path.toString());
      Properties prop = new Properties();
      prop.load(input);
      String property = prop.getProperty("updateRate");
      System.out.println("property="+property);
      int updateRate = Integer.parseInt(property);
//      System.out.println(updateRate);
      input.close();
    } catch (IOException e) {
      e.printStackTrace(System.out);
    }
  }

  @Override
  public void run() {
    try {
      WatchService watcher = FileSystems.getDefault().newWatchService();
      Path directory = Paths.get(".");
      WatchKey watchKey = directory.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
      while (true) {
        WatchKey wk = watcher.take();
        for (WatchEvent<?> event : wk.pollEvents()) {
          Path changed = (Path) event.context();
          if (changed.toString().equals("radar.properties")) {
            try {
              readProperties();
            } catch (Exception e) {
              e.printStackTrace(System.out);
            }
          }
        }
        if (!watchKey.reset()) {
          break;
        }
      }
    } catch (IOException e) {
      e.printStackTrace(System.out);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    new ClientPoll().start();
    new Writer().start();
  }
}

class Writer extends Thread {
  @Override
  public void run() {
    try {
      for (int i=0;i<10;i++) {
        File f = new File("radar.properties");
        FileWriter fw = new FileWriter(f);
        fw.write("updateRate="+i*1000);
        fw.close();
        sleep(1000L);
      }
    } catch (Exception e) {
      e.printStackTrace(System.out);
    }
    System.out.println("exit");
    System.exit(0);
  }
}
包装cl;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.io.InputStream;
导入java.nio.file.FileSystems;
导入java.nio.file.Path;
导入java.nio.file.path;
导入java.nio.file.StandardWatchEventTypes;
导入java.nio.file.WatchEvent;
导入java.nio.file.WatchKey;
导入java.nio.file.WatchService;
导入java.util.Properties;
公共类ClientPoll扩展了线程{
私有void readProperties(){
试一试{
Path Path=Path.get(“radar.properties”);
InputStream输入=新文件InputStream(path.toString());
Properties prop=新属性();
道具载荷(输入);
String property=prop.getProperty(“updateRate”);
System.out.println(“property=“+property”);
int updateRate=Integer.parseInt(属性);
//System.out.println(updateRate);
input.close();
}捕获(IOE异常){
e、 printStackTrace(系统输出);
}
}
@凌驾
公开募捐{
试一试{
WatchService watcher=FileSystems.getDefault().newWatchService();
Path directory=Path.get(“.”);
WatchKey WatchKey=directory.register(watcher,StandardWatchEventTypes.ENTRY\u MODIFY);
while(true){
WatchKey wk=watcher.take();
for(WatchEvent事件:wk.pollEvents()){
Path changed=(路径)event.context();
if(changed.toString().equals(“radar.properties”)){
试一试{
readProperties();
}捕获(例外e){
e、 printStackTrace(系统输出);
}
}
}
如果(!watchKey.reset()){
打破
}
}
}捕获(IOE异常){
e、 printStackTrace(系统输出);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
公共静态void main(字符串[]args){
新建ClientPoll().start();
新编写器().start();
}
}
类编写器扩展线程{
@凌驾
公开募捐{
试一试{

对于(int i=0;ii如果你能将你的代码和bug简化为合理的,你很可能会很快得到一个合理的答案。这仍然不是一个-我们无法编译和运行它。请查看@JonSkeet评论和我的评论中的链接,因为它将解释我们的请求以及为什么创建一个可以帮助你1)在此处获得答案,或2)你自己更容易发现问题的根源你似乎在使用一个永无止境的紧密循环来不断轮询属性——为什么?@hovercraftfullofels每当文件发生更改时,我都需要更新更新率——我想不出什么不同的东西这是checkProperties中的NumberFormatException。谢谢。
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 {
  private void readProperties() {
    try {
      Path path = Paths.get("radar.properties");
      InputStream input =new FileInputStream(path.toString());
      Properties prop = new Properties();
      prop.load(input);
      String property = prop.getProperty("updateRate");
      System.out.println("property="+property);
      int updateRate = Integer.parseInt(property);
//      System.out.println(updateRate);
      input.close();
    } catch (IOException e) {
      e.printStackTrace(System.out);
    }
  }

  @Override
  public void run() {
    try {
      WatchService watcher = FileSystems.getDefault().newWatchService();
      Path directory = Paths.get(".");
      WatchKey watchKey = directory.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
      while (true) {
        WatchKey wk = watcher.take();
        for (WatchEvent<?> event : wk.pollEvents()) {
          Path changed = (Path) event.context();
          if (changed.toString().equals("radar.properties")) {
            try {
              readProperties();
            } catch (Exception e) {
              e.printStackTrace(System.out);
            }
          }
        }
        if (!watchKey.reset()) {
          break;
        }
      }
    } catch (IOException e) {
      e.printStackTrace(System.out);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    new ClientPoll().start();
    new Writer().start();
  }
}

class Writer extends Thread {
  @Override
  public void run() {
    try {
      for (int i=0;i<10;i++) {
        File f = new File("radar.properties");
        FileWriter fw = new FileWriter(f);
        fw.write("updateRate="+i*1000);
        fw.close();
        sleep(1000L);
      }
    } catch (Exception e) {
      e.printStackTrace(System.out);
    }
    System.out.println("exit");
    System.exit(0);
  }
}