File file = new File("d:\\1.txt");    FileInputStream is = new FileInputStream(file);
    FileOutputStream os = new FileOutputStream(file);
    System.out.println(is.read());
代码如上,非常简单,此处的输出为-1,而且在进行了读操作之后,file的内容被清空。如上的代码有什么问题?

解决方案 »

  1.   

    FileInputStream     FileOutputStream 这两个不能同时对同一个文件操作。
      

  2.   

    建立流就算对文件进行操作了吗? 我仅仅是运行了上述的代码,file里的数据就全没了。
      

  3.   

    File file = new File("d:\\1.txt"); 是有数据的,
    但是我这里的输出为-1 意思就是没有读取出数据,为什么会这样?
      

  4.   

    FileOutputStream os = new FileOutputStream(file,true); 
      

  5.   

        FileOutputStream os = new FileOutputStream(file);
    就这句话,jdk这样解释:
     Creates a file output stream to write to the file represented by 
          the specified <code>File</code> object. A new 
         <code>FileDescriptor</code> object is created to represent this 
          file connection.
    就是往指定的文件里面写入数据,在开始写之前会将文件里面的内容清空。
    在实际的项目开发中,file不用我们先在磁盘上面创建,只需给个文件路径参数就够了。