还是有关 io 操作的 问题,怎么解?
题目:从键盘中输入文件的内容和要保存的文件的地址,将内容保存到该文件中;我的代码如下,运行结果不行啊(我是自学的,代码难免有不规范之处,望指点。)
package cn;
import
import
import
import
import java.util.Scanner;
public class TestDemo3 {
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
Scanner scan = null;
System.out.println("请输入文件名:");
scan = new Scanner(System.in);
File file = new File(scan.next());
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
OutputStream output = new FileOutputStream(file, true);
System.out.println("请输入内容:");
scan = new Scanner(System.in);
scan.useDelimiter("\n");
while (scan.hasNext() && scan.next() != "exit") {
output.write(scan.next().getBytes());
scan = new Scanner(System.in);
scan.useDelimiter("\n");
}
output.close();
System.exit(0);
}
}