JAVA NullpointerException FileInputStream(文件文件)
我有一个问题无法自己解决。
我是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();
}
}
在上面的代码片段中,我认为文件没有问题。 无论是否有异常,文件状态始终为 true,并且 file.length 为完整字节。 但是,虽然无限循环,如果我复制 &从另一个目录粘贴到 intDir 时,出现“NullPointerException”。
当Thread.sleep(time)超过1000ms时,不会发生NPE。 由于处理时间的原因,我想删除“Thread.sleep()”代码。
如果程序启动前intDir中已经有文件,则程序没有问题(不会发生NPE)
我想检查文件或FileInputStream状态是否发生NPE。
谢谢您的关心。
I have a problem not to solve by myself.
I'm a biginner in JAVA.
I don't know solution about this problem. But I think that I know when this problem occurs.
So, I aleady have solution about this problem. But I want another solution because my solution has another problem too. I have to reduce the process time.
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();
}
}
In above snippet, I think file has no problem.
file state is always true and file.length is full byte regardless Exception.
But, while infinite Loop, If I copy & paste from another Directory to the intDir , "NullPointerException" occurs.
When Thread.sleep(time) is over 1000ms, NPE doesn't occur.
I want to delete "Thread.sleep()" code because of process time.
If there are files in the intDir aleady before program start, Program has No problem (it doesn't occur NPE)
I want to check file or FileInputStream state not to occur NPE.
Thank you for your concern.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的问题很难理解,但我可以告诉你一个事实,这是不可能得到:
如果第179行是这一行:
以下之一必须是:
Your question is hard to understand, but I can tell you for a fact that it is impossible to get:
if line 179 is this line:
One of the following must be:
您可能迟早会用完文件句柄。完成后关闭 FileInputstream。
除此之外,解释一下你真正想用你的代码做什么(而不是让 CPU 使用率达到最高)。
You probably run out of file handles sooner or later. Close the FileInputstream when you are finished with it.
Besides that, explain what you really want to do with your code (instead of driving CPU usage to the top).
怎么样:
我做了几件事——
1.摆脱了不必要的文件名生成
2.最后尝试释放文件句柄资源
顺便说一句,我的猜测是您的睡眠允许终结器运行。
How about this:
I did a couple of things --
1. got rid of unnecessary file name generation
2. put a try finally to release file handle resources
BTW, my guess is that your sleep allowed finalizers to run.