文件编写器和空格?
我被要求编写一个作业,其中将提示用户输入键和/或值。
到目前为止,这是我的代码:
import java.util.Scanner;
import java.io.*;
class bTree
{
//Fields
static Scanner input = new Scanner(System.in);
static boolean done = false;
public static void main(String args[])throws Exception
{
FileWriter fWriter = new FileWriter("data.txt");
do
{
System.out.print("Enter command: ");
String enter[] = input.nextLine().split(" ", 3);
if(enter[0].toLowerCase().equals("insert"))
{
fWriter.write(enter[1] + "\n" + enter[2] + "\n");
fWriter.flush();
}
else if(enter[0].toLowerCase().equals("select"))
{
FileReader fReader = new FileReader("data.txt");
Scanner fileInput = new Scanner(fReader);
while(fileInput.hasNext() && done == false)
{
if(fileInput.nextLine().equals(enter[1]))
{
System.out.println(fileInput.nextLine());
done = true;
}
else
{
fileInput.nextLine();
}
}
done = false;
}
else if(enter[0].toLowerCase().equals("update"))
{
fWriter.write(enter[2]);
fWriter.flush();
}
else if(enter[0].toLowerCase().equals("exit"))
{
System.exit(0);
}
}
while(true);
}
}
问题:当我打开 data.txt 时,没有空格。因此,如果我在记事本中输入“插入 1001 gen”和“10001 genny”,它将显示为“1001gen10001genny”。有什么建议吗?
I was asked to write an assignment wherein the user would be prompted to input a key and/or a value.
So far, here is my code:
import java.util.Scanner;
import java.io.*;
class bTree
{
//Fields
static Scanner input = new Scanner(System.in);
static boolean done = false;
public static void main(String args[])throws Exception
{
FileWriter fWriter = new FileWriter("data.txt");
do
{
System.out.print("Enter command: ");
String enter[] = input.nextLine().split(" ", 3);
if(enter[0].toLowerCase().equals("insert"))
{
fWriter.write(enter[1] + "\n" + enter[2] + "\n");
fWriter.flush();
}
else if(enter[0].toLowerCase().equals("select"))
{
FileReader fReader = new FileReader("data.txt");
Scanner fileInput = new Scanner(fReader);
while(fileInput.hasNext() && done == false)
{
if(fileInput.nextLine().equals(enter[1]))
{
System.out.println(fileInput.nextLine());
done = true;
}
else
{
fileInput.nextLine();
}
}
done = false;
}
else if(enter[0].toLowerCase().equals("update"))
{
fWriter.write(enter[2]);
fWriter.flush();
}
else if(enter[0].toLowerCase().equals("exit"))
{
System.exit(0);
}
}
while(true);
}
}
Problem: When i open the data.txt, there are no spaces. So if i enter "insert 1001 gen" and "10001 genny", in notepad, it would come out as "1001gen10001genny". Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是
notepad.exe
对行结尾很挑剔,并且有很多可能性。当您将"\n"
写入FileWriter
时,它会写入一个字符,即'\n'
。但记事本需要序列"\r\n"
。它将单个“\n”
显示为无。这是您的代码,稍作修改以解决一些陷阱。
备注:
PrintWriter
而不是FileWriter
来获得正确的行结尾。select
命令,我在使用后关闭了fReader
。enter[0].toLowerCase()
。toLowerCase
的正确变体。select
命令,使其更加简洁。The problem is that
notepad.exe
is picky about line endings, and there are many possibilities. When you write"\n"
to aFileWriter
, it writes a single character, namely'\n'
. But notepad expects the sequence"\r\n"
instead. It shows a single"\n"
as nothing.Here is your code, slightly modified to work around some pitfalls.
Remarks:
PrintWriter
instead of aFileWriter
to get the line endings correct.select
command I closed thefReader
after using it.enter[0].toLowerCase()
multiple times.toLowerCase
.select
command to be a little more concise.问题是 String Enter[] = input.nextLine().split(" ", 3); ,它杀死了空格。因此,每次使用
fWriter.write
时,请在每个数组条目后附加一个空格或写入一个附加的“”。看这里
The problem is
String enter[] = input.nextLine().split(" ", 3);
, it kills the Spaces. So append a space after each array entry or write an additional " " everytime you usefWriter.write
.look here
如前所述,换行符对于记事本来说是不正确的。或者,您可以将该 FileWriter 包装在 BufferedWriter 中,并使用 newLine 方法始终插入正确的换行符。
As already stated the line feed character is incorrect for notepad. Alternatively you could wrap that FileWriter in a BufferedWriter and use the newLine method to always insert the correct line feed.
我认为您正在 UNIX 中运行您的程序。在unix系统中“\r\n”是换行符。
如果您在 Windows 中运行程序,我认为该文件应该包含类似这样的内容。
1001
创
10001
创
I think you are running your program in UNIX. In unix system "\r\n" is the line feed.
If you are running your program in Windows, I think the file should contain something like this.
1001
gen
10001
genny