Java如何读取回车

在Java中,读取回车的方法取决于我们使用的是哪种输入流。Java提供了多种输入流,包括System.in、FileInputStream、BufferedReader等。每种输入流都有不同的读取方式。

1. 使用System.in读取回车

在Java中,可以使用System.in来读取标准输入。但是,System.in是一个字节流,因此我们需要将其包装在一个InputStreamReader中,以便可以按字符读取。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("请输入一行文本:");
            String line = reader.readLine(); // 读取一行文本,包括回车符
            System.out.println("你输入的是:" + line);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

以上代码中,我们通过使用BufferedReader和InputStreamReader来读取System.in。readLine()方法可以读取一行文本,包括回车符。在输入文本后按回车键,程序将会继续执行,并打印出输入的文本。

2. 使用FileInputStream读取回车

如果我们需要从文件中读取回车符,我们可以使用FileInputStream。和System.in不同,FileInputStream是一个字节流,因此我们需要将其包装在一个InputStreamReader中,以便可以按字符读取。

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line); // 输出每一行文本,包括回车符
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

以上代码中,我们通过使用BufferedReader、InputStreamReader和FileInputStream来读取文件中的内容。在循环中,我们逐行读取文本,并输出每一行,包括回车符。

3. 使用Scanner读取回车

Java还提供了Scanner类,可以用于从不同的输入源中读取数据,包括回车符。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); // 使用System.in作为输入源
        System.out.println("请输入一行文本:");
        String line = scanner.nextLine(); // 读取一行文本,包括回车符
        System.out.println("你输入的是:" + line);
        scanner.close();
    }
}

以上代码中,我们使用Scanner类从标准输入中读取一行文本,包括回车符。nextLine()方法可以读取一行文本,直到遇到回车符。在输入文本后按回车键,程序将会继续执行,并打印出输入的文本。

总结

以上是几种常见的读取回车的方法。根据不同的需求,我们可以选择使用System.in、FileInputStream、BufferedReader或者Scanner来读取回车符。通过使用适当的输入流和相关方法,我们可以轻松地读取回车符并进行处理。

代码示例

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        try {
            // 使用System.in读取回车
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("请输入一行文本:");
            String line = reader.readLine(); // 读取一行文本,包括回车符
            System.out.println("你输入的是:" + line);
            
            // 使用FileInputStream读取回车
            reader = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line); // 输出每一行文本,包括回车符
            }
            reader.close();
            
            // 使用Scanner读取回车
            Scanner scanner = new Scanner(System.in); // 使用System.in作为输入源
            System.out.println("请输入一行文本:");
            String line = scanner.nextLine(); // 读取一行文本,包括回车符
            System.out.println("你输入的是:" + line);
            scanner.close();