2015-06-23 39 views
5

我有一个类PDF,它实现了一个接口fileReader在Java中使用try-catch时变量范围的问题

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 

public class PDF implements fileReader { 
    @Override 
    public byte[] readFile(File pdfDoc) { 
     if (!pdfDoc.exists()) { 
      System.out.println("Could not find" + pdfDoc.getName() + " on the specified path"); 
      return null; 
     } 
     FileInputStream fin = null; 
     try { 
      fin = new FileInputStream(pdfDoc); 
     } catch (FileNotFoundException e) { 
      System.out.println(""); 
      e.printStackTrace(); 
     } 
     byte fileContent[] = new byte[(int) pdfDoc.length()]; 
     try { 
      fin.read(fileContent); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return fileContent; 
    } 
} 

import java.io.File; 
public interface fileReader { 
    <T> T readFile(File fileObject); 
} 

我注意到有变量fin范围的问题。

我做了另一种实现是:

public byte[] readFile1(File pdfDoc) { 
     if (!pdfDoc.exists()) { 
      System.out.println("Could not find" + pdfDoc.getName() + " on the specified path"); 
      return null; 
     } 
     FileInputStream fin = null; 
     try { 
      fin = new FileInputStream(pdfDoc); 
      byte fileContent[] = new byte[(int) pdfDoc.length()]; 
      try { 
       fin.read(fileContent); 
      } catch (IOException e) { 
       System.out.println(""); 
       e.printStackTrace(); 
      } 
     } catch (FileNotFoundException e) { 
      System.out.println(""); 
      e.printStackTrace(); 
     } 
     return fileContent; 
    } 

但现在我无法访问fileContent

如何组合try-catches以便我没有范围问题? 对这个问题有没有更好的设计方法?我必须为阅读三种不同类型的文件做功能。

+2

你的范围问题到底是什么? –

回答

5

由于Java 7,你可以按照如下结合try-catch

FileInputStream fin = null; 
    try { 
     fin = new FileInputStream(pdfDoc); 
     byte fileContent[] = new byte[(int) pdfDoc.length()]; 
     fin.read(fileContent); 
    } catch (IOException | FileNotFoundException e) { 
     System.out.println(""); 
     e.printStackTrace(); 
    } 

其中,在我看来,使代码更清洁,变量的作用域更为明显。

+1

噢,不错,我比我的建议更好。我不知道这是可能的。学习新的东西:) – DrZoo

+1

@DrZoo:这是Java的美妙之处,它使你的脚趾;) – StuPointerException

+0

我试过这个,但我得到了'多个catch语句中的替代方法无法通过继承关联 Alternative java.io.FileNotFoundException是另一个java.io.IOException'的子类。我在Intellij –

3

可以嵌套在try catch语句:

try { 
     FileInputStream fin = new FileInputStream(pdfDoc); 
     byte fileContent[] = new byte[(int) pdfDoc.length()]; 
     try { 
      fin.read(fileContent); 
      return fileContent; 
     } catch (IOException e) { 
     e.printStackTrace(); 
     } finally { 
     fin.close(); 
     } 

    } catch (FileNotFoundException e) { 
     System.out.println(""); 
     e.printStackTrace(); 
    } 
    return null; 

注意,我在最后条款中加入了密切的()进行清理。并且在出现错误的情况下,返回null可能不是您想要的,但这是特定于应用程序的。

+0

我明白了。我曾尝试过那样做,但方式不对。谢谢 –

2

您可以有一个try与多个catch块。

try { 
    //do stuff 
} 
catch (FileNotFoundException e) { 
     System.out.println(""); 
     e.printStackTrace(); 
} 
catch (IOException e) { 
     e.printStackTrace(); 
} 
2

您可以修改此部分:

 FileInputStream fin = null; 
     try { 
      fin = new FileInputStream(pdfDoc); 
     } catch (FileNotFoundException e) { 
      System.out.println(""); 
      e.printStackTrace(); 
     } 
     byte fileContent[] = new byte[(int) pdfDoc.length()]; 
     try { 
      fin.read(fileContent); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

通过

{ 
...... 
     FileInputStream fin = null; 
     byte fileContent[]=null; 
     try { 
      fin = new FileInputStream(pdfDoc); 
      fileContent = new byte[(int) pdfDoc.length()]; 
      fin.read(fileContent); 
     } catch (FileNotFoundException e) { 
      System.out.println(""); 
      e.printStackTrace(); 
     }catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return fileContent 
    } 
+0

这取决于Java版本,如果您正在使用java 7或更新的版本,您只能使用一个catch块(如catch)(IOException | FileNotFoundException e) –

1

我会写这样的:

public byte[] readFile(File pdfDoc) { 
    if (!pdfDoc.exists()) { 
     System.out.println("Could not find" + pdfDoc.getName() + " on the specified path"); 
     return null; 
    } 
    FileInputStream fin = null; 
    byte fileContent[] = new byte[(int) pdfDoc.length()]; 

    try { 
     fin = new FileInputStream(pdfDoc); 
     fin.read(fileContent); 
    } catch (FileNotFoundException e) { 
     System.out.println(""); 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     if (null != fin) { 
      fin.close(); 
     } 
    } 
    return fileContent; 
}
0

由于Java 7,有一个很好的实用方法用于读取文件的全部内容:

return Files.readAllBytes(pdfFile.toPath()); 

此方法将为您打开和关闭FileInputStream,因此您不需要自己执行此操作。如果出现错误,它会抛出IOException。通常情况下,最好还是让这个异常传播给调用者,但如果你真的想在这种情况下返回null,你可以做到这一点,如下所示:

try { 
    return Files.readAllBytes(pdfFile.toPath()); 
} catch (IOException e) { 
    e.printStackTrace(); 
    return null; 
} 

这也有很好的优势,该值在返回这种情况是明确的 - 或者如果文件不能再被找到,你是否真的想要返回一个填充0值的数组?

请注意,由于NoSuchFileException是IOException的子类,因此catch块将处理这两者。如果你想以不同的方式处理它,你可以写的NoSuchFileException一个单独的catch块:

try { 
    return Files.readAllBytes(pdfFile.toPath()); 
} catch (NoSuchFileException e) { 
    System.err.println("Oh no, the file has disappeared."); 
    e.printStackTrace(); 
    return null; 
} catch (IOException e) { 
    System.err.println("The file exists, but could not be read."); 
    e.printStackTrace(); 
    return null; 
} 

最后,我也许应该提到你的文件读取的代码不正确,如InputStream.read()不一定读整个文件一次。这就是为什么它会返回读取的字节数,因此您可以为文件的其余部分再次调用它。但正如我所说,从Java 7开始,您不需要使用如此低级别的API(除非该文件太大而无法放入内存中)。