FileOutputStream

chsunnyboy 2014-08-09 11:24:29
为什么一下两段代码效果一样,哪位高手给解释一下

String filename="C://Users//Administrator//Desktop//极速传输//1.script";
File file=new File(filename);
try {
PrintStream printStream = new PrintStream(file);
System.setOut(printStream);
} catch (FileNotFoundException e) {

e.printStackTrace();
}
String filename="C://Users//Administrator//Desktop//极速传输//1.script";
File file=new File(filename);
FileOutputStream fileOutputStream;
try {
fileOutputStream = new FileOutputStream(file);
PrintStream printStream = new PrintStream(fileOutputStream);
System.setOut(printStream);
} catch (FileNotFoundException e) {

e.printStackTrace();
}
...全文
161 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
KJZL 2014-08-09
  • 打赏
  • 举报
回复
效果一样,但实现的方式和效率不一样,一个是用打印流传输文件,一个是用文件输出流传输文件。
百里马 2014-08-09
  • 打赏
  • 举报
回复
引用 5 楼 chsunnyboy 的回复:
楼上最后的私有方法是什么意思啊 private PrintStream(boolean autoFlush, OutputStream out) { super(out); this.autoFlush = autoFlush; this.charOut = new OutputStreamWriter(this); this.textOut = new BufferedWriter(charOut); }
这个才是真正的构造方法,被私有化的 对外提供的构造方法,最后都是通过这个方法来初始化的
chsunnyboy 2014-08-09
  • 打赏
  • 举报
回复
楼上最后的私有方法是什么意思啊 private PrintStream(boolean autoFlush, OutputStream out) { super(out); this.autoFlush = autoFlush; this.charOut = new OutputStreamWriter(this); this.textOut = new BufferedWriter(charOut); }
百里马 2014-08-09
  • 打赏
  • 举报
回复

    public PrintStream(File file) throws FileNotFoundException {
        this(false, new FileOutputStream(file));
    }

    public PrintStream(OutputStream out) {
        this(out, false);
    }

    public PrintStream(OutputStream out, boolean autoFlush) {
        this(autoFlush, requireNonNull(out, "Null output stream"));
    }

    private PrintStream(boolean autoFlush, OutputStream out) {
        super(out);
        this.autoFlush = autoFlush;
        this.charOut = new OutputStreamWriter(this);
        this.textOut = new BufferedWriter(charOut);
    }
两者确实一样,因为你传file进去,它内部也会创建一个FileOutputStream
zhjdg 2014-08-09
  • 打赏
  • 举报
回复
看源代码就知道。 最终差别就在boolean autoFlush 第一true 第二false
private PrintStream(boolean autoFlush, OutputStream out) {
        super(out);
        this.autoFlush = autoFlush;
        this.charOut = new OutputStreamWriter(this);
        this.textOut = new BufferedWriter(charOut);
    }

62,616

社区成员

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

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