4-包装流

包装流,构造函数以流为参数的,即包装流,又称处理流。

下面示例,PrintWriter、PrintStream、Scanner


关键点:

1、PrintStream主要用来输出字节流,PrintWriter主要用来输出字符流

2、Scanner一般用来输出键盘输入的信息

package com.wlt;

import org.junit.Test;

import java.io.*;
import java.util.Scanner;

/**
 * 复习包装类
 * Created by wlt on 2018/2/24.
 */
public class ReviewPackage {
    public static void main(String[] args){
       new ReviewPackage().scanner();
    }

    @Test
    public void printstream() throws FileNotFoundException,IOException{
        File file = new File("printstream.txt");
        try (
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                PrintStream printStream = new PrintStream(fileOutputStream);
                ) {
            printStream.write("无解字节".getBytes());
            printStream.println("无解字符串");
            printStream.println(new tmp("111","222"));
            printStream.append("无解");
        }
    }
    @Test
    public void printwriter() throws IOException {
        File file = new File("printwriter.txt");
        try (
                //文件如果不存在会自动创建
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                PrintWriter printWriter = new PrintWriter(fileOutputStream);
                ) {
            printWriter.write("无解writer\n");
            printWriter.println("无解字符串");
            printWriter.println(new tmp("111","222"));
            printWriter.append("无解");
        }
    }
    public void scanner(){
        try (
                Scanner scanner = new Scanner(System.in);
                ) {
            System.out.println(scanner.next());
        }

    }
}
class tmp{
    private String name;
    private String age;

    public tmp(String name, String age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独行侠_阿涛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值