Java IO流-标准输入输出流
发布日期:2022-04-02 18:15:45 浏览次数:7 分类:博客文章

本文共 1445 字,大约阅读时间需要 4 分钟。

2017-11-05 19:13:21

标准输入输出流:System类中的两个成员变量。

标准输入流(public static final InputStream in):“标准”输入流。此流已打开并准备提供输入数据。通常,此流对应于键盘输入或者由主机环境或用户指定的另一个输入源。

                      InputStream is = System.in;

标准输出流(public static final PrintStream out):“标准”输出流。此流已打开并准备接受输出数据。通常,此流对应于显示器输出或者由主机环境或用户指定的另一个输出目标。

                      PrintStream ps = System.out;

  • System.out

标准输出流本质是PrintStream类,下面来看一下PrintStream类的一些方法。

*常用方法

public static void main(String[] args) {        System.out.println("hello world.");                //获取标准输出流对象        PrintStream ps = System.out;        ps.println("hello world");    }

使用字符缓冲流进行包装(很少用,因为不方便):

public static void main(String[] args) throws IOException {        BufferedWriter bw= new BufferedWriter(new OutputStreamWriter(System.out));        bw.write("hello");        bw.newLine();        bw.write("world");        bw.newLine();        bw.write("!");        bw.flush();        bw.close();    }
  • System.in

标准输入流本质是InputStream类。

键盘录入的方法

    A:main方法接收参数

    B:Scanner类

      Scanner sc = new Scanner(System.in)

      int x = sc.nextInt()

    C:通过字符缓冲流包装标准输入流,BufferedReader类

      BufferedReader br = new BufferedReader(new InputStreamReader(System.in))

 

public static void main(String[] args) throws IOException {        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));     // 读到末尾返回null        String line = br.readLine();        int i = Integer.parseInt(line);        System.out.println(i);    }

 

转载地址:https://www.cnblogs.com/hyserendipity/p/7788399.html 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Java IO流-字符流
下一篇:Java IO流-随机访问流

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月23日 19时54分10秒