我有一个问题不能自己解决。
我是 JAVA 的大佬。
我不知道这个问题的解决方案。但我想我知道这个问题何时发生。所以,我已经解决了这个问题。但我想要另一个解决方案,因为我的解决方案也有另一个问题。我必须减少处理时间。
String intDir="C:\\RNE_IN";
while(true) {
File interfaceDirectory = new File(intDir);
String[] arrayfiles = interfaceDirectory.list(new FBMFileFilter());
for(String f : arrayfiles){
String filename = String.format("%1$s%2$s%3$s", intDir,File.separator,f);
File file = new File(filename);
FileInputStream stream = null;
System.out.println(file.canExecute()); // true
System.out.println(file.canRead()); // true
System.out.println(file.exists()); // true
System.out.println(file.isFile()); // true
System.out.println(file.length()); // call full bytes of file
// I can control NPE with this Thread sleep Time.
Thread.sleep(1);
// It occurs when Stream is constructed in the below.
stream = new FileInputStream(file);
FBMDeviceOnlyParser onlyparser = new FBMDeviceOnlyParser();
onlyparser.ParseDeviceNameOnly(stream);
String onlydevice = onlyparser.getDeviceName();
String onlystepseq = onlyparser.getStepSeq();
}
}
在上面的片段中,我认为文件没有问题。文件状态始终为真,并且 file.length 是完整字节,无论异常。但是,在无限循环中,如果我从另一个目录复制并粘贴到 intDir ,则会发生“NullPointerException”。
当 Thread.sleep(time) 超过 1000ms 时,NPE 不会发生。由于处理时间,我想删除“Thread.sleep()”代码。
如果在程序启动前intDir中有文件,则程序没有问题(不会发生NPE)
我想检查文件或 FileInputStream 状态是否不会发生 NPE。
谢谢你的关心。