java - FileWriter.write() 有没有办法在运行时继续在空格后写入字符串?
问题描述
当我试图运行这件作品时,
import java.io.*;
import java.util.Scanner;
import static java.lang.System.*;
class CSWrite1
{
public static void main(String[] args) throws IOException
{
Scanner input = new Scanner(in);
out.print("Enter the filename\t>");
String file = input.next();
out.println("Enter the text");
String text = input.next(); // IN:"Hello, How are you" --> "Hello,
try(FileWriter fw = new FileWriter(file))
{ fw.write(text); }
}
}
在将文本输入为“你好,你好吗”时,文件只写有“你好,”。第一个空格之后的剩余文本不会被写入文件中。
解决方案
以下对我有用:
import static java.lang.System.*;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class CSWrite1 {
public static void main(String[] args) {
try (Scanner input = new Scanner(in)) {
out.print("Enter file name> ");
String file = input.nextLine();
try (FileWriter fw = new FileWriter(file)) {
out.print("Enter text: ");
String text = input.nextLine(); // IN:"Hello, How are you" --> "Hello,
fw.write(text);
}
catch (IOException xIo) {
xIo.printStackTrace();
}
}
}
}
推荐阅读
- python - 如何在 Django 测试中模拟序列化程序?
- python - ttk.Notebook 分离标签
- linux - 在 bash 脚本中将 *.txt.csv 文件重命名为 .csv
- r - 我可以在 UI 的一部分中使用单选按钮选择来创建标题吗?
- javascript - React Hook Form 未正确发布请求
- cakephp-3.0 - cakephp 3 在指定路由中默认查看 Json
- sql - Entity Framework Core 5 位小数 | .NET 5
- mysql - aws 教育 RDS - 设置 MYSQL 数据库时出错
- awk - 用冒号分隔时使用 awk 来划分列
- python - 合并具有重叠线性范围的记录