首页 > 解决方案 > java - 如何使用Java中的Scanner从标准输入中读取任意数量的行

问题描述

我正在寻找扫描标准输入并将输入的内容写入文件。

目前,我的代码看起来像这样

import java.util.*;
import java.io.*;

public class ShoppingList {

    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("Invalid number of arguments.");
            return;
        }

        String outputFile = args[0];

        try {
            Scanner scanIn = new Scanner(System.in);
            File fileOut = new File(outputFile);
            PrintWriter myWriter = new PrintWriter(fileOut);
            fileOut.createNewFile();

            while (true) {
                String nextLine = scanIn.nextLine();
                if (nextLine.equals("")) {
                    break;
                }
                myWriter.write(nextLine + "\n");
            }
            myWriter.close();
        } catch (IOException e) {
            System.err.println("IOException");
            return;
        }
        
    }
}

目前,我的代码在输入流的结尾和程序的结尾之间留下了一个空白行。Link有什么办法可以去掉那条线吗?谢谢!

标签: javajava.util.scanner

解决方案


写入文件时不要将换行符 ( \n) 添加到输入的末尾,例如:

myWriter.write(nextLine + "\n");

而是在将用户输入字符串添加到文件之前将其添加到文件中。是的,这将在第一个用户输入行写入文件之前添加一个空行,因此您需要一种方法来确定是否已将一行写入文件。有很多方法可以做到这一点,计数器,布尔标志等。

使用布尔标志:

public class ShoppingList {

    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("Invalid number of arguments.");
            return;
        }

        String outputFile = args[0];

        try {
            Scanner scanIn = new Scanner(System.in);
            File fileOut = new File(outputFile);
            java.io.PrintWriter myWriter = new java.io.PrintWriter(fileOut);
            fileOut.createNewFile();

            // Boolean Flag to determine if first line is written to file.
            boolean firstLineWritten = false; 

            while (true) {
                String nextLine = scanIn.nextLine().trim();
                if (nextLine.equals("")) {
                    // Nothing provided by User - Close the file.
                    break;
                }
                // If a first line has been written...
                if (firstLineWritten) {
                    // Add a system line separator to the file line previously written.
                    myWriter.write(System.lineSeparator());
                }
                // Add User Input to file (no newline character added)
                myWriter.write(nextLine);
                // Write the User input to the file right away!
                myWriter.flush(); 
                // Flag the fact that the first line is written.
                firstLineWritten = true; 
            }
            myWriter.close();
        } catch (IOException e) {
            System.err.println("IOException");
        }
    }
}

使用计数器:

public class ShoppingList {

    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("Invalid number of arguments.");
            return;
        }

        String outputFile = args[0];

        try {
            Scanner scanIn = new Scanner(System.in);
            File fileOut = new File(outputFile);
            java.io.PrintWriter myWriter = new java.io.PrintWriter(fileOut);
            fileOut.createNewFile();

            // A counter to determine if first line is written to file. 
            int lineCounter = 0; 

            while (true) {
                String nextLine = scanIn.nextLine().trim();
                if (nextLine.equals("")) {
                    // Nothing provided by User - Close the file.
                    break;
                }
                // If a first line has been written...
                if (lineCounter > 0) {
                    // Add a system line separator to the file line previously written.
                    myWriter.write(System.lineSeparator());
                }
                // Add User Input to file (no newline character added)
                myWriter.write(nextLine);
                // Write the User input to the file right away!
                myWriter.flush(); 
                // Increment counter to the fact that the first line is written.
                lineCounter++; 
            }
            myWriter.close();
        } catch (IOException e) {
            System.err.println("IOException");
        }
    }
}

推荐阅读