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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黄树人 中级黑马   /  2012-8-27 22:48  /  2358 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 黄树人 于 2012-8-28 00:02 编辑
  1. public class PrintWriterDemo {
  2.         public static void main(String[] args) throws IOException {
  3.                
  4.                 method();
  5.         }

  6.         private static void method() throws IOException {
  7.                 PrintWriter pw = new PrintWriter(new FileOutputStream("a.txt"),true);
  8.                
  9.                
  10.                 pw.print("helloworld");
  11.                

  12.         }
  13. }        
复制代码
打印流PrintWriter有提供自动刷新的功能 PrintWriter(OutputStream out, boolean autoFlush)
所以按理上面程序不加pw.close();,应该也可以把helloworld写入 a.txt中啊
可是运行完程序,a.txt中神马都木有啊,求解

评分

参与人数 1技术分 +1 收起 理由
包晗 + 1 加油

查看全部评分

8 个回复

倒序浏览
有自动刷新功能。但是,需要开启自动刷新,并且只有在println,printf,format方法的时候有效。

public class PrintWriterDemo {
        public static void main(String[] args) throws IOException {
               
                method();
        }

        private static void method() throws IOException {
                PrintWriter pw = new PrintWriter(new FileOutputStream("a.txt"),true);               
                                pw.println("helloworld");                       
}

希望对你有帮助!

评分

参与人数 1技术分 +1 收起 理由
包晗 + 1 25分也要经常来论坛

查看全部评分

回复 使用道具 举报
不加close() 需要调用flush()刷新     


package com.ruantong.intrew.bank;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

public class PrintWriterDemo {

        public static void main(String[] args) throws IOException {

               

                method();

        }



        private static void method() throws IOException {

                PrintWriter pw = new PrintWriter(new FileOutputStream("F:/a.txt"),true);
                pw.print("helloworld");
                pw.flush();//不加close() 需要调用flush()刷新        }
        
}        

点评

去申请改名,不改名不加分的哦。 这次先加 速度改名哟  发表于 2012-8-28 11:06

评分

参与人数 1技术分 +1 收起 理由
包晗 + 1 加油

查看全部评分

回复 使用道具 举报
你少了ln
pw.print("helloworld");
这句应该写成 pw.println("helloworld");才会自动刷新
回复 使用道具 举报
版主大人看到给分哦
回复 使用道具 举报
将pw.print("helloworld");改成 pw.println("helloworld");
在附上JDK关于该类的说明:
向文本输出流打印对象的格式化表示形式。此类实现在 PrintStream 中的所有 print 方法。它不包含用于写入原始字节的方法,对于这些字节,程序应该使用未编码的字节流进行写入。
与 PrintStream 类不同,如果启用了自动刷新,则只有在调用 println、printf 或 format 的其中一个方法时才可能完成此操作,而不是每当正好输出换行符时才完成。这些方法使用平台自有的行分隔符概念,而不是换行符。

public PrintWriter(OutputStream out,boolean autoFlush)
参数:
out - 输出流
autoFlush - boolean 变量;如果为 true,则 println、printf 或 format 方法将刷新输出缓冲区

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

回复 使用道具 举报
你的pw.print("helloworld");要改成pw.println("helloworld");
或者pw.print("helloworld");pw.flush();
或者pw.print("helloworld");pw.close();
打印流PrintWriter是有提供自动刷新的功能
但是只有在调用println()、printf()、format()方法的 时候自动刷新。
回复 使用道具 举报
杨卓儒 黑马帝 2012-8-28 00:09:03
8#
println()、printf()、format()。牢记这3
回复 使用道具 举报
追梦黑马 发表于 2012-8-27 23:01
不加close() 需要调用flush()刷新     

谢谢提醒 我说我回答那么多问题 怎么没反应呢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马