java - 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有什么办法可以去掉那条线吗?谢谢!
解决方案
写入文件时不要将换行符 ( \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");
}
}
}
推荐阅读
- python - 错误:SECRET_KEY 设置不能为空 - Django webdev
- java - Java - 有序映射
- scala - 火花数据框过滤器和选择
- soap - 骆驼 cxf 消费服务
- firebase - Firebase Realtime Security - 为某些逻辑编写规则
- c - 浮点异常 CS50 可读性赋值
- c - 如何修复数组、标识符和初始化程序错误?
- python-3.x - serial.serialutil.SerialException: 无法打开端口 'COM4': PermissionError(13, 'Acceso denegado.', None, 5)?
- flask - 缓存会在多大程度上破坏我的 Flask 应用程序?
- python-3.x - 如何以月(字符串)日(整数)年(整数)的形式获取日期?