文件复印机正在打印我的整个摘录,我希望它在用户输入之间逐行打印

File copier is printing out my whole excerpt, I want it to do it line by line with user input between

我需要我的程序逐行打印此文件,等待用户在每行之间按回车键。我的代码不断打印整个摘录。我需要更改什么?

import java.io.*;
import java.util.*;
public class NoteCopier {
    public static void main(String[] args) throws IOException {
    System.out.println("Hello! I copy an excerpt to the screen line for line"
                        + " just press enter when you want a new line!");
    try {
        File file = new File("excerpt.txt");
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader inreader = new InputStreamReader(fis);
        BufferedReader reader = new BufferedReader(inreader);
        String line = reader.readLine();
        Scanner scan = new Scanner(System.in);
        
        while(true) {
                String scanString = scan.nextLine();
                if(line != null) {
                if(scanString.isEmpty()){
                    System.out.println(line);
                    line = reader.readLine();
                }
                else {
                    scanString = null;
                    break;
                }
            
            }
        }
    }
        
    catch(IOException e) {
        e.printStackTrace();
    }
    }
}

如果行为空,你将永远循环;嵌套的 if 语句。

我在新的 Stream 风格中做到了,没有 System.in 上无处不在但不必要的扫描仪。

private void dump(String file) {
    Path path = Paths.get(file);
    BufferedReader con = new BufferedReader(new InputStreamReader(System.in));
    try (Stream<String> in = Files.lines(path, Charset.defaultCharset())) {
        AtomicInteger lineCounter = new AtomicInteger();
        in.forEach(line -> {
            System.out.println(line);

            if (lineCounter.get() == 0) {
                String input = null;
                try {
                    input = con.readLine();
                } catch (IOException e) {
                }
                if (input == null) {
                    throw new IndexOutOfBoundsException();
                } else if (input.equals(" ")) {
                    lineCounter.set(10);
                }
            } else {
                lineCounter.decrementAndGet();
            }
        });
    } catch (IndexOutOfBoundsException e) {
        System.out.println("< Stopped.");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

使用 CtrlD 我相信你可以在 Windows 退出。 我添加了带有 Space 的行将转储接下来的 10 行。

丑陋的是用户输入行。


使用 java.io.Console 可以使用字符串提示询问输入,然后可用于打印文件的行作为提示。

private void dump(String file) {
    Path path = Paths.get(file);
    Console con = System.console();
    try (Stream<String> in = Files.lines(path, Charset.defaultCharset())) {
        AtomicInteger lineCounter = new AtomicInteger();
        in.forEach(line -> {
            if (lineCounter.get() == 0) {
                //String input = con.readLine("%s |", line);
                String input = new String(con.readPassword("%s", line));
                if (input == null) {
                    throw new IndexOutOfBoundsException();
                } else if (input.equals(" ")) {
                    lineCounter.set(10);
                }
            } else {
                System.out.println(line);
                lineCounter.decrementAndGet();
            }
        });
    } catch (IndexOutOfBoundsException e) {
        System.out.println("< Stopped.");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

使用带有文件行的提示,并询问一个非回显的“密码”就足够了。您仍然需要 Enter.

有一个问题:您必须运行这是真正的命令行。 IDE 中的“控制台”使用 System.setIn,这将导致空控制台。我只是创建一个 .bat/.sh 文件。否则 System.out.print(line); System.out.flush(); 可能适用于某些操作系统。