(求助) PrintStream(File file)的疑惑

神经猪 2012-11-09 04:56:44
使用 PrintStream 打印数据到文件,一般都使用下面的方法:
API文档中描述为:PrintStream(OutputStream out) 参接收OutputStream对象,实例化PrintStream类
File f = new File("D:\\a.txt") 
// 向"D:\\a.txt"文件中打印内容"hello"
PrintStream ps = new PrintStream(new FileOutputStream(f));
ps.print("hello");


为什么不直接使用下面的代码呢?也有构造方法:PrintStream(File file) 直接通过一个File对象实例化PrintStream类。
File f = new File("D:\\a.txt") 
// 向"D:\\a.txt"文件中打印内容"hello"
PrintStream ps = new PrintStream(f);
ps.print("hello");


2种方法都可以正常在文件中打印内容。不知道有没有人了解两者的区别。
新人不材,请大家指教!
...全文
156 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
huntor 2012-11-09
  • 打赏
  • 举报
回复
为什么不使用PrintWriter。PrintStream这个会自动转码。
引用
All characters printed by a PrintStream are converted into bytes using the platform's default character encoding. The PrintWriter class should be used in situations that require writing characters rather than bytes.
MiceRice 2012-11-09
  • 打赏
  • 举报
回复
没啥区别,你看看Java的源码就知道了: public PrintStream(File file) throws FileNotFoundException { this(false, new FileOutputStream(file)); init(new OutputStreamWriter(this)); }

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧