public static void main(String[] args) throws IOException{
File file=new File("e:/test.txt");
FileInputStream fis=new FileInputStream(file);
FileOutputStream fos=new FileOutputStream(file);

int i=-1;
while((i=fis.read())!=-1)
System.out.println(i);
fis.close();
fos.close();
}
上面这段程序中test.txt有一些文字。但是程序一运行不但什么都打印不出来,而且test.txt也变成了空文件。问题是我并没有让fos.write()写点什么进去呀。希望大侠们教我,这是为什么妮!

解决方案 »

  1.   

    这样就好了
      public static void main(String[] args) throws IOException{
            File file=new File("e:/test.txt");
            FileInputStream fis=new FileInputStream(file);
          //  FileOutputStream fos=new FileOutputStream(file);
            
            int i=-1;
            while((i=fis.read())!=-1)
                System.out.println(i);
            fis.close();
         //   fos.close();
        }
      

  2.   

    FileOutputStream
    public FileOutputStream(String name,
                            boolean append)
                     throws FileNotFoundException创建一个向具有指定 name 的文件中写入数据的输出文件流。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。创建一个新 FileDescriptor 对象来表示此文件连接。 
    首先,如果有安全管理器,则用 name 作为参数调用 checkWrite 方法。 如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开它,则抛出 FileNotFoundException。 
    参数:
    name - 与系统有关的文件名
    append - 如果为 true,则将字节写入文件末尾处,而不是写入文件开始处 
    抛出: 
    FileNotFoundException - 如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开它。 
    SecurityException - 如果存在安全管理器,且其 checkWrite 方法拒绝对文件进行写入访问。
    从以下版本开始: 
    JDK1.1 
    另请参见:
    SecurityManager.checkWrite(java.lang.String)调用
            FileOutputStream fos=new FileOutputStream(file,true);就不会了貌似...
      

  3.   

    复制错...应该看这个..FileOutputStream
    public FileOutputStream(File file)
                     throws FileNotFoundException创建一个向指定 File 对象表示的文件中写入数据的文件输出流。创建一个新 FileDescriptor 对象来表示此文件连接。 
    首先,如果有安全管理器,则用 file 参数表示的路径作为参数来调用 checkWrite 方法。 如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开,则抛出 FileNotFoundException。 
    参数:
    file - 为了进行写入而打开的文件。 
    抛出: 
    FileNotFoundException - 如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开 
    SecurityException - 如果存在安全管理器,且其 checkWrite 方法拒绝对文件进行写入访问。
    另请参见:
    File.getPath(), SecurityException, SecurityManager.checkWrite(java.lang.String)
      

  4.   

    问题出在FileOutputStream fos=new FileOutputStream(file);这句代码上
    如果换成下面的顺序你就可以读出文件内容public static void main(String[] args) throws Exception{

    File file=new File("e:/test.txt");
            FileInputStream fis=new FileInputStream(file);
            int i=-1;
            while((i=fis.read())!=-1)
                System.out.println(i);
            fis.close();
            
            FileOutputStream fos=new FileOutputStream(file);
            fos.close();
    }原因:当执行这句话时,底层调用  public FileOutputStream(File file) throws FileNotFoundException {
    this(file, false);
        }
    那个false是append的默认值,false说明不是在原来文件内容的基础上扩展,而是用新内容覆盖原来的内容,但是因为你没使用fos流输入任何内容,所以文件变为空。
      

  5.   

    恩,看了一下源码,FileOutputStream(file,false),会调用一个private native openAppend(String name)的私有本地方法,也就是用append=false的方式打开File,很可能首先清空了文件。可惜看不到JVM调用本地方法的代码。