import java.io.*;
import java.util.*;class shiyan
{
public static void main(String[] agrs) throws IOException
{
PrintWriter out=new PrintWriter(new FileWriter("d.txt"));
out.println("hsiydfskdkfs kfsdkfksdfksdfsdjdfsdkj");
}
}
为什么这段代码能通过编译和运行,而且在当前目录下能生成d.txt文件,但println中的字符串却写不进去??

解决方案 »

  1.   

    最后要加上out.flush();out.close();就可以了。
      

  2.   

    import java.io.*;
    import java.util.*;class shiyan
    {
    public static void main(String[] agrs) throws IOException
    {
    PrintWriter out=new PrintWriter(new FileWriter("d.txt"));
    out.println("hsiydfskdkfs kfsdkfksdfksdfsdjdfsdkj");
    out.flush();
    }
    }
    就可以了哈
      

  3.   

    加上了flush,println也没写进去东西,同意smy,用writeflush是把没写到文件里的流里的数据全写进去
      

  4.   

    import java.io.*;
    import java.util.*;class shiyan
    {
    public static void main(String[] agrs) throws IOException
    {
    PrintWriter out=new PrintWriter(new FileWriter("d.txt"));

    out.write("hsiydfskdkfs kfsdkfksdfksdfsdjdfsdkj");
    out.close();
    }
    }
    成功了!!!
      

  5.   

    老大你没有close掉!要记得调用 out.close()。