简体   繁体   English

I / O类的例外

[英]Exception for classes I/O

I'm studying for OCP exam these classes I/O: 我正在为以下课程I / O学习OCP考试:

  • OutputStream subclasses: OutputStream子类:

    • FileOutputStream FileOutputStream中
    • BufferedOutputStream 的BufferedOutputStream
    • DataOutputStream DataOutputStream类
    • PrintStream (I see its methods write() don't throw exceptions) PrintStream(我看到它的方法write()不会抛出异常)
  • Writer subclasses: 作家子类:

    • FileWriter 的FileWriter
    • BufferedWriter 但是BufferedWriter
    • PrintWriter (I see its methods write() don't throw exceptions) PrintWriter(我看到它的方法write()不会抛出异常)
  • Reader subclasses: 读者子类:

    • FileReader 的FileReader
    • BufferedReader 的BufferedReader
  • InputStream subclasses: InputStream子类:

    • BufferedInputStream 的BufferedInputStream
    • FilterInputStream FilterInputStream中
    • ObjectInputStream ObjectInputStream的

My question: For the main methods read and write (with different signatures), when does each of these classes throw the IOException? 我的问题:对于主要的读写方法(具有不同的签名),这些类中的每一个何时会引发IOException? In the javadoc there is only a phrase: 在javadoc中只有一个短语:

IOException - if an I/O error occurs IOException-如果发生I / O错误

without an explanation about the cases. 没有对案件的解释。

I know this (I don't know if these are correct): 我知道这一点(我不知道这些是否正确):

  • FileOutputStream throws java.io.FileNotFoundException if the file doesn't exist. 如果文件不存在,则FileOutputStream引发java.io.FileNotFoundException。 This class, infact, doesn't create a file; 实际上,此类不会创建文件。

  • FileWriter throws java.io.FileNotFoundException if the file doesn't exist. 如果文件不存在,则FileWriter引发java.io.FileNotFoundException。 This class infact, doesn't create a file; 此类实际上并没有创建文件;

  • BufferedReader throws java.nio.file.NoSuchFileException (subclass of IOException), if file doesn't exist; 如果文件不存在,则BufferedReader抛出java.nio.file.NoSuchFileException(IOException的子类);否则,抛出Exception。

  • FileInputStream throws java.io.FileNotFoundException if the file doesn't exist. 如果文件不存在,则FileInputStream引发java.io.FileNotFoundException。

Thanks a lot! 非常感谢!

A. 一种。

If you google the API documentation for the class, generally the Javadocs will give an explanation of what exceptions are thrown in what circumstances, eg for FileWriter 如果您搜索该类的API文档,通常Javadocs会解释在什么情况下会抛出哪些异常,例如FileWriter

https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html

An I/O exception is thrown by any of these classes when they encounter a problem during execution. 当这些类在执行过程中遇到问题时,它们会抛出I / O异常。 The file output streams can throw an exception if : 如果出现以下情况,文件输出流可能会引发异常:

  • File don't exist 文件不存在
  • File is read only 文件是只读的
  • The OS refuses access to the file 操作系统拒绝访问文件
  • The drive you are writing to dose not have sufficient space 您要写入的驱动器没有足够的空间

The input streams usually throws exception if : 如果以下情况,输入流通常会引发异常:

  • The stream is not properly initialised 流未正确初始化
  • The input type is miss matched 输入类型未匹配
  • If the file beeing read from is changed while reading 如果读取时读取文件蜂鸣声
  • If the file doesn't exist. 如果文件不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM