A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© sd110572 金牌黑马   /  2013-12-9 20:09  /  1175 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 sd110572 于 2013-12-10 00:06 编辑

写不进去,文件最后还是0个字节!
  1. import java.io.Console;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.PrintWriter;
  5. import java.util.Date;
  6. import java.util.Scanner;


  7. public class Empty
  8. {
  9.         public static void main(String[]args)
  10.         {
  11.                 File file = new File("C:\\Users\\Administrator\\Desktop\\20.txt");
  12.                 try
  13.                 {
  14.                         PrintWriter out = new PrintWriter( file );
  15.                         out.print("ff");
  16.                 }
  17.                 catch(         FileNotFoundException e)
  18.                 {
  19.                 }
  20.         }
  21. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
简★零度 + 1

查看全部评分

5 个回复

倒序浏览
请刷新流,out.flush(),或者关闭流,如果你既不刷新也不关闭流,这样会使数据“残留”在缓冲区中!
回复 使用道具 举报
因为你创建的是一个不带自动刷新的printWriter ,要用flush()刷新,这样就可以了
  1. public class Empty
  2. {
  3.         public static void main(String[]args)
  4.         {
  5.                 File file = new File("1.txt");
  6.                 try
  7.                 {
  8.                        
  9.                         PrintWriter out = new PrintWriter(file);
  10.                         out.println("ff");
  11.                         out.flush();
  12.                 }
  13.                 catch(         FileNotFoundException e)
  14.                 {
  15.                 }
  16.         }
  17. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
简★零度 + 1

查看全部评分

回复 使用道具 举报
你应该在打印后关闭流out.close()或者刷新流out.flush()就行
回复 使用道具 举报
本帖最后由 范二青年 于 2013-12-10 22:02 编辑
  1. <div class="blockcode"><blockquote>
复制代码
回复 使用道具 举报
  1. class Empty
  2. {
  3.        public static void main(String[]args)
  4.        {
  5.                File file = new File("1.txt");
  6.                PrintWriter out = null;
  7.                try
  8.                {
  9.                       out = new PrintWriter(file);
  10.                        out.println("ff");
  11.                        out.flush();
  12.                }
  13.                catch(         FileNotFoundException e)
  14.                {
  15.                }finally{
  16.                     out.close();
  17.               }
  18.        }
  19. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
简★零度 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马