如何使用Java中的Scanner从标准输入读取任意行数:

huangapple go评论102阅读模式
英文:

How to read an arbitrary amount of lines from standard input using Scanner in java

问题

  1. import java.util.*;
  2. import java.io.*;
  3. public class ShoppingList {
  4. public static void main(String[] args) {
  5. if (args.length != 1) {
  6. System.err.println("参数数量无效。");
  7. return;
  8. }
  9. String outputFile = args[0];
  10. try {
  11. Scanner scanIn = new Scanner(System.in);
  12. File fileOut = new File(outputFile);
  13. PrintWriter myWriter = new PrintWriter(fileOut);
  14. fileOut.createNewFile();
  15. while (true) {
  16. String nextLine = scanIn.nextLine();
  17. if (nextLine.equals("")) {
  18. break;
  19. }
  20. myWriter.write(nextLine + "\n");
  21. }
  22. myWriter.close();
  23. } catch (IOException e) {
  24. System.err.println("IOException");
  25. return;
  26. }
  27. }
  28. }

我的代码在输入流的末尾和程序末尾之间会留下一个空行。链接 有没有办法去掉那行空行?谢谢!

英文:

I'm looking to scan standard input and write what is inputted into a file.

Currently, my code looks like this

  1. import java.util.*;
  2. import java.io.*;
  3. public class ShoppingList {
  4. public static void main(String[] args) {
  5. if (args.length != 1) {
  6. System.err.println("Invalid number of arguments.");
  7. return;
  8. }
  9. String outputFile = args[0];
  10. try {
  11. Scanner scanIn = new Scanner(System.in);
  12. File fileOut = new File(outputFile);
  13. PrintWriter myWriter = new PrintWriter(fileOut);
  14. fileOut.createNewFile();
  15. while (true) {
  16. String nextLine = scanIn.nextLine();
  17. if (nextLine.equals("")) {
  18. break;
  19. }
  20. myWriter.write(nextLine + "\n");
  21. }
  22. myWriter.close();
  23. } catch (IOException e) {
  24. System.err.println("IOException");
  25. return;
  26. }
  27. }
  28. }

Currently, my code leaves a blank line between the end of the input stream and the end of the program. Link Is there any way to get rid of that line? Thanks!

答案1

得分: 1

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

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

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

使用布尔标志:

  1. public class ShoppingList {
  2. public static void main(String[] args) {
  3. if (args.length != 1) {
  4. System.err.println("参数数量无效。");
  5. return;
  6. }
  7. String outputFile = args[0];
  8. try {
  9. Scanner scanIn = new Scanner(System.in);
  10. File fileOut = new File(outputFile);
  11. java.io.PrintWriter myWriter = new java.io.PrintWriter(fileOut);
  12. fileOut.createNewFile();
  13. // 用于确定是否已经写入第一行的布尔标志。
  14. boolean firstLineWritten = false;
  15. while (true) {
  16. String nextLine = scanIn.nextLine().trim();
  17. if (nextLine.equals("")) {
  18. // 用户未提供内容 - 关闭文件。
  19. break;
  20. }
  21. // 如果已经写入了第一行...
  22. if (firstLineWritten) {
  23. // 在先前写入的文件行中添加系统换行符。
  24. myWriter.write(System.lineSeparator());
  25. }
  26. // 将用户输入添加到文件(不添加换行符)
  27. myWriter.write(nextLine);
  28. // 立即将用户输入写入文件!
  29. myWriter.flush();
  30. // 标记第一行已经写入。
  31. firstLineWritten = true;
  32. }
  33. myWriter.close();
  34. } catch (IOException e) {
  35. System.err.println("IOException");
  36. }
  37. }
  38. }

使用计数器:

  1. public class ShoppingList {
  2. public static void main(String[] args) {
  3. if (args.length != 1) {
  4. System.err.println("参数数量无效。");
  5. return;
  6. }
  7. String outputFile = args[0];
  8. try {
  9. Scanner scanIn = new Scanner(System.in);
  10. File fileOut = new File(outputFile);
  11. java.io.PrintWriter myWriter = new java.io.PrintWriter(fileOut);
  12. fileOut.createNewFile();
  13. // 用于确定是否已经写入第一行的计数器。
  14. int lineCounter = 0;
  15. while (true) {
  16. String nextLine = scanIn.nextLine().trim();
  17. if (nextLine.equals("")) {
  18. // 用户未提供内容 - 关闭文件。
  19. break;
  20. }
  21. // 如果已经写入了第一行...
  22. if (lineCounter > 0) {
  23. // 在先前写入的文件行中添加系统换行符。
  24. myWriter.write(System.lineSeparator());
  25. }
  26. // 将用户输入添加到文件(不添加换行符)
  27. myWriter.write(nextLine);
  28. // 立即将用户输入写入文件!
  29. myWriter.flush();
  30. // 增加计数器以标记第一行已经写入。
  31. lineCounter++;
  32. }
  33. myWriter.close();
  34. } catch (IOException e) {
  35. System.err.println("IOException");
  36. }
  37. }
  38. }
英文:

Don't add the newline (\n) to the end of the input when you write to file such as:

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

Instead add it to the file before you add the User input string to file. Yes, this would add a blank line before the first User input line is written to file so you need a means to determine if a line has already been written to the file. There are a number of ways you can do this, a counter, a boolean flag, etc.

Using a Boolean Flag:

  1. public class ShoppingList {
  2. public static void main(String[] args) {
  3. if (args.length != 1) {
  4. System.err.println("Invalid number of arguments.");
  5. return;
  6. }
  7. String outputFile = args[0];
  8. try {
  9. Scanner scanIn = new Scanner(System.in);
  10. File fileOut = new File(outputFile);
  11. java.io.PrintWriter myWriter = new java.io.PrintWriter(fileOut);
  12. fileOut.createNewFile();
  13. // Boolean Flag to determine if first line is written to file.
  14. boolean firstLineWritten = false;
  15. while (true) {
  16. String nextLine = scanIn.nextLine().trim();
  17. if (nextLine.equals("")) {
  18. // Nothing provided by User - Close the file.
  19. break;
  20. }
  21. // If a first line has been written...
  22. if (firstLineWritten) {
  23. // Add a system line separator to the file line previously written.
  24. myWriter.write(System.lineSeparator());
  25. }
  26. // Add User Input to file (no newline character added)
  27. myWriter.write(nextLine);
  28. // Write the User input to the file right away!
  29. myWriter.flush();
  30. // Flag the fact that the first line is written.
  31. firstLineWritten = true;
  32. }
  33. myWriter.close();
  34. } catch (IOException e) {
  35. System.err.println("IOException");
  36. }
  37. }
  38. }

Using a Counter:

  1. public class ShoppingList {
  2. public static void main(String[] args) {
  3. if (args.length != 1) {
  4. System.err.println("Invalid number of arguments.");
  5. return;
  6. }
  7. String outputFile = args[0];
  8. try {
  9. Scanner scanIn = new Scanner(System.in);
  10. File fileOut = new File(outputFile);
  11. java.io.PrintWriter myWriter = new java.io.PrintWriter(fileOut);
  12. fileOut.createNewFile();
  13. // A counter to determine if first line is written to file.
  14. int lineCounter = 0;
  15. while (true) {
  16. String nextLine = scanIn.nextLine().trim();
  17. if (nextLine.equals("")) {
  18. // Nothing provided by User - Close the file.
  19. break;
  20. }
  21. // If a first line has been written...
  22. if (lineCounter > 0) {
  23. // Add a system line separator to the file line previously written.
  24. myWriter.write(System.lineSeparator());
  25. }
  26. // Add User Input to file (no newline character added)
  27. myWriter.write(nextLine);
  28. // Write the User input to the file right away!
  29. myWriter.flush();
  30. // Increment counter to the fact that the first line is written.
  31. lineCounter++;
  32. }
  33. myWriter.close();
  34. } catch (IOException e) {
  35. System.err.println("IOException");
  36. }
  37. }
  38. }

huangapple
  • 本文由 发表于 2020年9月11日 06:53:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63838651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定