简体   繁体   English

Java读取一个带有多个换行符的文本输入

[英]Java read one text input with multiple newline characters

I'm looking for a way to read a line that is copied and pasted into an IDE or a terminal/cmd, such that a BufferedReader will read all of the text even when it encounters a newline character ('\\n). 我正在寻找一种读取复制并粘贴到IDE或终端机/ cmd中的行的方法,以便BufferedReader即使遇到换行符('\\ n)也会读取所有文本。 The tricky part is that the reader will have to know that the user has pressed enter (which is a newline), but it has to keep reading all of the characters in the input string until it has reached the last '\\n' . 棘手的部分是,读者将必须知道用户已按下Enter键(这是换行符),但是它必须继续读取输入字符串中的所有字符,直到到达最后一个'\\n'为止。

Is there any way to do this (such as with an InputStreamReader or something)? 有什么方法可以做到这一点(例如使用InputStreamReader等)?

ANSWER: 回答:

public static void main(String[] args) {
    InputStreamReader reader = new InputStreamReader(System.in);
    StringBuilder sb = new StringBuilder();

    int ch;

    System.out.println("Paste text below (enter or append to text \"ALT + 1\" to exit):");

    try {
        while ((ch = reader.read()) != (char)63 /*(char)63 could just be ☺*/) {
            sb.append(ch);
        }
        reader.close();
    }catch (IOException e) {
        System.err.println(e.toString());
    }
    String in = sb.toString();
    System.out.println(in);
}

Well, I didn't see this on Stack Overflow, so I thought I would ask, and I didn't know if InputStreamReader would work... but I guess it does. 好吧,我在Stack Overflow上没有看到这个,所以我想问一下,而且我不知道InputStreamReader是否可以工作...但是我想它可以。 So, if you ever want to read text that has a bunch of newline characters, you have to use InputStreamReader . 因此,如果您想读取包含一串换行符的文本,则必须使用InputStreamReader

This is a class implements Reader (which implements readable and closeable and has a method ( read(chBuf) ) that allows you to read an input stream to the buffer characters (an array), chBuf. The code that I used to test this is as follows: 这是一个实现Reader的类(该类实现了readablecloseable并具有一个方法( read(chBuf) ),该方法可让您读取缓冲字符(数组)chBuf的输入流。我用来测试该代码的代码是如下:

public static void main(String[] args) {
    String in = "";

    InputStreamReader reader = new InputStreamReader(System.in);

    System.out.println("paste text with multiple newline characters below:");

    try {
        char chs[] = new char[1000];
        int n;
        while (reader.read(chs) != -1) {
            for (int i = 0; i < chs.length; i++) {
                char ch = chs[i];
                if (ch == '\u0000') {
                    break;
                } 
                in += chs[i];
            }
            System.out.println(in);
        }
        System.out.print(".");
    } catch (IOException e) {
        System.err.println(e.toString());
    }

    System.out.println(in);
}

This works... sort of. 这行得通... It prints the inputted text one and a half times. 它将打印输入的文本一遍半。

I can see you've made an effort, so I'll show you what I mean. 我可以看到您已经在努力,所以我将告诉您我的意思。 This is a pretty common pattern when you need to read from a stream: 当您需要从流中读取时,这是一种非常常见的模式:

char[] buffer = new char[1000];
StringBuilder sb = new StringBuilder();
int count;
// note this loop condition
while((count = reader.read(buffer)) != -1) {
    sb.append(buffer, 0, count);
}
String input = sb.toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM