上码import java.io.*;
public class In { /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("Test.txt")));
out.writeBytes("somebody \n");
 }catch(IOException e) {
 System.out.println("No File!!!!!!!!");
 }
}
}我用eclipse运行了下    确实生成了个Test.txt     但里边啥都没  不死心 我又用javac编译了下    运行后还是只生成Test.txt文件 为什么呢 ??? 我明明输入了somebody \n 啊????
刚学IO 。可能问题蠢了点  呵呵

解决方案 »

  1.   

     try
            {
                DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("Test.txt")));
                out.writeBytes("somebody \n");
                out.flush();        }
            catch (IOException e)
            {
                System.out.println("No File!!!!!!!!");
            }
      

  2.   

    用到了BufferedOutputStream缓冲流,要么flush,要么close,才能写入到文件中
      

  3.   

    IO里面绝大部分流是有缓冲机制,也就是先将数据写到缓冲区,缓冲区满了之后才真正写到文件
    如果你数据量小,没有占满缓冲区,没有显示地执行flush()或者close()方法,那么数据不会写到文件还有,IO部分操作的时候要有个好习惯,打开流之后一定记得关闭流,用try-catch-finally
    /**
     * Created by IntelliJ IDEA.
     * User: admin
     * Date: 2011-10-10
     * Time: 11:40:58
     * To change this template use File | Settings | File Templates.
     */
    import java.io.*;
    public class In {    /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            DataOutputStream out=null;
            try{
                out= new DataOutputStream(new BufferedOutputStream(new FileOutputStream("Test.txt")));
                out.writeBytes("somebody \n");
                out.flush();        }catch(IOException e) {
                e.printStackTrace();
    //            System.out.println("No File!!!!!!!!");
            }finally{
                try{
                    out.close();
                }catch(IOException e){
                    e.printStackTrace();
                }        }
        }
    }
      

  4.   

    flush close() 加上应该可以
    首先要清空缓冲区里的数据 再关闭流
      

  5.   

    package com.qq.server.model;
    import java.io.*;
    public class MyQqServer 
    {    
    public static void main(String args[])
    {
    File f=new File("d:/t.txt");
    FileOutputStream fos=null;
    byte[] b=new byte[1024];
    int n=0;
    try {
    fos=new FileOutputStream(f);
    String s="aa ";
    fos.write(s.getBytes());

    } catch (Exception e) {
    e.printStackTrace();
    }finally{
    try {
    fos.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    }
      

  6.   

    请问是所有的IO流都有缓冲机制吗?难道不是因为我们用了BufferedInputStream、BufferedOutputStream、BufferedWrite、BufferedRead这四个只后才有了缓冲吗?
      

  7.   

    为什么调用close和flush我查了下jdk api文档  我这里最外一层是DataOutputStream  他的方法没有close只有flush   那为什么大家说可以调用close呢
      

  8.   


    有close方法,从 java.io.FilterOutputStream 中继承过来的
    在方法摘要列表下面有个小框框里有这么一段  从类 java.io.FilterOutputStream 继承的方法 
    close, write 
      从类 java.lang.Object 继承的方法 
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait 
      从接口 java.io.DataOutput 继承的方法 
    write 
      

  9.   

    请问为什么要先勇flush清空缓冲区 再关闭流   清空了缓冲区数据不就全没了   如何还会写入到文件中?
      

  10.   

    使用 close 就行,close 会调 flush,没必要自己再去调。
      

  11.   

    flush 是将缓冲区数据写入底层流再清空缓冲区。