JAVA NullpointerException FileInputStream(文件文件)

发布于 2024-10-27 09:36:45 字数 1643 浏览 5 评论 0原文

我有一个问题无法自己解决。
我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟酒忠诚 2024-11-03 09:36:46

你的问题很难理解,但我可以告诉你一个事实,这是不可能得到:

java.lang.NullPointerException at Apeiron.MainEntry.main(MainEntry.java:179) Exception in thread "main" 
java.lang.NullPointerException at Apeiron.MainEntry.main(MainEntry.java:260)

如果第179行是这一行:

stream = new FileInputStream(file);

以下之一必须是:

  • 你给了我们一个不完整的堆栈跟踪,或者
  • 您告诉我们异常的错误位置,或者
  • 您实际上根本没有执行该代码;例如,您在更改代码后没有正确重建代码。

Your question is hard to understand, but I can tell you for a fact that it is impossible to get:

java.lang.NullPointerException at Apeiron.MainEntry.main(MainEntry.java:179) Exception in thread "main" 
java.lang.NullPointerException at Apeiron.MainEntry.main(MainEntry.java:260)

if line 179 is this line:

stream = new FileInputStream(file);

One of the following must be:

  • you have given us an incomplete stack trace, or
  • you've told us the incorrect location of the exception, or
  • you are not actually executing that code at all; e.g. you've not rebuilt the code properly after changing it.
紅太極 2024-11-03 09:36:46

您可能迟早会用完文件句柄。完成后关闭 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).

可爱咩 2024-11-03 09:36:46

怎么样:

String intDir="C:\\RNE_IN";  
File interfaceDirectory = new File(intDir);
while(true) {
  for(File file : interfaceDirectory.listFiles(new FBMFileFilter())) {
    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 

    final FileInputStream stream = new FileInputStream(file);
    try {
      FBMDeviceOnlyParser onlyparser = new FBMDeviceOnlyParser(); 
      onlyparser.ParseDeviceNameOnly(stream);    
      String onlydevice = onlyparser.getDeviceName();
      String onlystepseq = onlyparser.getStepSeq();
    } finally {
      stream.close();
    }
  }
}

我做了几件事——
1.摆脱了不必要的文件名生成
2.最后尝试释放文件句柄资源

顺便说一句,我的猜测是您的睡眠允许终结器运行。

How about this:

String intDir="C:\\RNE_IN";  
File interfaceDirectory = new File(intDir);
while(true) {
  for(File file : interfaceDirectory.listFiles(new FBMFileFilter())) {
    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 

    final FileInputStream stream = new FileInputStream(file);
    try {
      FBMDeviceOnlyParser onlyparser = new FBMDeviceOnlyParser(); 
      onlyparser.ParseDeviceNameOnly(stream);    
      String onlydevice = onlyparser.getDeviceName();
      String onlystepseq = onlyparser.getStepSeq();
    } finally {
      stream.close();
    }
  }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文