输入流和输出流

标准的输入和输出

标准的输入

System.in

package com.itxs.demo03;

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

/**
 * @Classname : demo01
 * @Description : TODO 标准输入
 * @Author : lin_refuel@qq.com
 */
public class demo01 {
    public static void main(String[] args) throws IOException {
             //标准输入System,in
        //TODO InputStream in = System.in;//是一个输入类
          //输入类封装成为一个字符输入类
      //TODO  InputStreamReader ir = new InputStreamReader(in);
        //字符输入类进行缓冲
      //  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //TODO*******************************************************
        //调用br里面的readline方法,可以进行输入
       // String scr;
       // System.out.println(scr);
      //TODO******************************************************************
        //把内容输入到文件里面
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:/JAVA/ce/newTest.txt"));
        String scr;
        while(true){
            System.out.print("请输入:");
           scr = new Scanner(System.in).nextLine();
           if(scr.equals("over"))
           // if((scr = br.readLine()).equals("over"))
                break;
            bw.write(scr);//写入
            bw.flush();//刷新
            bw.newLine();//换行
        }

        bw.close();
      // br.close();
    }
}

Scanner采集字符

next() :获取字符串会被空格,回车,缩进给截取
nextLine():不会被上面的影响直接被获取

Scanner匿名对象和实例化对象的使用

package com.itxs.demo03;

import java.util.Scanner;

/**
 * @Classname : demo02
 * @Description : TODO Scanner 匿名对象的使用
 * @Author : lin_refuel@qq.com
 */
public class demo02 {
    public static void main(String[] args) {
        String name;//姓名
        int age;//年龄
        String sex;//性别
        float score;//成绩
        //**********************************************************
        //实例化对象Scanner的使用
//TODO        Scanner sc = new Scanner(System.in);
//TODO        System.out.println("请输入姓名");
//TODO        name = sc.nextLine();
//TODO        System.out.println("请输入年龄");
//TODO        age = sc.nextInt();
//TODO        sc.nextLine(); //清空缓存(吸收回车)
//TODO        System.out.println("请输入性别");
//TODO        sex = sc.nextLine();
//TODO        System.out.println("请输入一个成绩");
//TODO        score = sc.nextFloat();
//TODO        System.out.printf("姓名:%s,年龄:%d,性别:%s,成绩:%.2f",name,age,sex,score);
        //*****************************************************************
        //匿名对象的使用
        System.out.println("请输入姓名");
        name = new Scanner(System.in).nextLine();
        System.out.println("请输入年龄");
        age = new Scanner(System.in).nextInt();
        System.out.println("请输入性别");
        sex = new Scanner(System.in).nextLine();
        System.out.println("请输入一个成绩");
        score = new Scanner(System.in).nextFloat();
        System.out.printf("姓名:%s,年龄:%d,性别:%s,成绩:%.2f",name,age,sex,score);
    }
}

标准的输出

System.out

package com.itxs.demo03;

import java.io.*;

/**
 * @Classname : demo
 * @Description : TODO 标准输出
 * @Author : lin_refuel@qq.com
 */
public class demo03 {
    public static void main(String[] args) throws IOException {
       //System.out
        PrintStream out = System.out;
        //封装成为一个字符流输出类
        OutputStreamWriter ow = new OutputStreamWriter(out);
        //封装成为缓冲字符流输出类
        BufferedWriter bw = new BufferedWriter(ow);
        //调用方法write输出到屏幕上
        bw.write("你好嘛,是的我很好");
        //三种常用的输出方式
        System.out.println("你真的很好");//自带换行
        System.out.print("哈哈哈哈");//手动添加换行
        System.out.printf("%d",24);//手动换行,可以格式化进行输出
        bw.close();
    }
}

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值