我正在尝试用 Java 创建一个完全简单的、无 GUI 的 HTML 编辑器。我所做的是创建一个“for”循环,其中用户输入一个字符串(即“str”),并将其打印到 [name].html 文件中(该名称之前已由用户决定) ,只要 'str' 不等于 'quit',在这种情况下程序就会结束。这是代码,不包括非常简单的命名部分:
public static void edit(String nameParam) throws FileNotFoundException {
//Creates the [name].html file
PrintStream write = new PrintStream(new File(nameParam + ".html"));
//puts the Name of the file at the top of the screen
s.pl(nameParam);
for(String str=scan.next(); !str.equalsIgnoreCase("quit");){
s.p("~");
write.println(str);
}
}
但是,它似乎不起作用。当我使用该程序时,它允许我在任意多行上键入我想要的任何内容,但不会在行开头打印“~”符号,并且不会将它们写入文件(但是,它确实如此)创建文件。)。当我强制退出程序时 - 没有内部方法可以关闭它,尽管应该有 - 它会打印一行永恒的 ~ 符号。
有什么帮助吗? 谢谢。
编辑: s.p == System.print; s.pl == System.println
最佳答案
与java中的其他流一样,写入流后需要flush(),完成流后需要close():
for (String str=scan.next(); !str.equalsIgnoreCase("quit");) {
s.p("~");
write.println(str);
write.flush();
}
//...
write.close();
关于java - 努力用 Java 制作 HTML 编辑器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18242349/