Java输入流Scanner/BufferedReader使用方法示例

1.使用Scanner

使用时需要引入包import java.util.Scanner;首先定义Scanner对象

Scanner sc = new Scanner(System.in);
如果要输入整数,则 int n = sc.nextInt();
String类型的,则String temp = sc.next();

比如:


import java.util.Scanner;

public class Test {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(scanner.next());
    }
}

2.使用BufferedReader

用前需要引入 import java.io.Reader;

BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
String input = br.readLine();

比如:


import java.io.*;

public class Test {

    public static void main(String[] args) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    System.in));
            System.out.println(br.readLine());
        } catch (IOException e) {

        }
    }
}

若文章对您有帮助,帮忙点个赞!

0
2
发布时间 2013-11-29 11:08:26
0 条回复(回复会通过微信通知作者)
点击加载更多评论
登录 后再进行评论
(微信扫码即可登录,无需注册)