栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

【Java】PrintStream和PrintWriter

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Java】PrintStream和PrintWriter


活动地址:CSDN21天学习挑战赛

1.PrintStream

实例

package com;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;

public class PrintWriter_ {
    public static void main(String[] args) throws IOException {
        PrintStream out = System.out;
        //在默认情况下,PrintStream 输出数据的位置是 标准输出,即显示器
        
        out.print("john, hello");
        //因为print底层使用的是write , 所以我们可以直接调用write进行打印/输出
        out.write("Demo龙,你好".getBytes());
        out.close();

        //我们可以去修改打印流输出的位置/设备
        //1. 输出修改成到 "e:\f1.txt"
        //2. "hello, 韩顺平教育~" 就会输出到 e:f1.txt
        //3. public static void setOut(PrintStream out) {
        //        checkIO();
        //        setOut0(out); // native 方法,修改了out
        //   }
        System.setOut(new PrintStream("e:\f1.txt"));
        System.out.println("hello, Demo龙龙龙~");

    }
}

演示结果

2.PrintWriter
package com;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

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

        //PrintWriter printWriter = new PrintWriter(System.out);
        PrintWriter printWriter = new PrintWriter(new FileWriter("e:\f2.txt"));
        printWriter.print("hi, Demo龙你好~~~~");
        printWriter.close();//flush + 关闭流, 才会将数据写入到文件..

    }
}

演示结果

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1038914.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号