下面是个有关读取文件的题目,请各位帮忙解决一下:
What does the following code do?File f=new File(“hello.test”);
FileOutputStream out = new FileOutputStream(f);Select the one right answer:
A.Create a new file named “hello.test ” if it does not yet exist.It also opens the file so you can write to it and read from it. 
B.Create a new file named “hello.test ” if it does not yet exist.The file is not opened.
C.Open a file named “hello.test” so that you can write to it and read from it ,but does not creat the file if it does not yet     exist.
D.Open a file named “hello.test” so that you can write to it but cannot read from it.
E.Create an object that you can now use to create and open the file named ”hello.test”,and write to and read from the file.

解决方案 »

  1.   

    c,因为还没有对文件进行操作,如out.write(1),这样文件就被创建起来了
      

  2.   

    A:创建一个叫“hello.test“的文件,如果不存在的话。它也为打开文件,能对此文件进行写和读操作。
    B: 如果不存在,创建一个叫“hello.test“的文件。文件没有被打开;
    C: 打开一个叫“hello.test“的文件,你能进行读和写操作,如果他不存在的话,你不能创建他;
    D:打开一个叫“hello.test“的文件,你只能写他不能读他;
    E: 创建一个既能读又能写的名字为hello.test的file对象,同时打开它;
    API:
    FileOutputStream
    public FileOutputStream(File file)
                     throws FileNotFoundException创建一个向指定 File 对象表示的文件中写入数据的文件输出流。创建一个新 FileDescriptor 对象来表示此文件连接。 
    首先,如果有安全管理器,则用 file 参数表示的路径作为参数来调用 checkWrite 方法。 如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开,则抛出 FileNotFoundException。 参数:
    file - 为了进行写入而打开的文件。 
    抛出: 
    FileNotFoundException - 如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开 
    SecurityException - 如果存在安全管理器,且其 checkWrite 方法拒绝对文件进行写入访问文件输出流是用于将数据写入 File 或 FileDescriptor 的输出流。文件是否可用或能否可以被创建取决于基础平台。特别是某些平台一次只允许一个 FileOutputStream(或其他文件写入对象)打开文件进行写入。在这种情况下,如果所涉及的文件已经打开,则此类中的构造方法将失败。 
    分析:
    c中不存在不能创建?
    FileNotFoundException - 如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开 ,API 中说明无法创建时会抛出FileNotFoundException异常,它还是进行了创建操作。我个人认为C是错误的应该选A: