[英]File not writing when java program is executed from command line
I have this code which executes perfectly from an IDE, making a new file and writing to it, but when I run it from command-line it won't write to a file. 我有这段代码可以从IDE完美执行,生成一个新文件并写入其中,但是当我从命令行运行它时,它不会写入文件。 It still prints the println and receives my scanner input, but it won't write a new file.
它仍会打印println并接收我的扫描仪输入,但不会写入新文件。
public class test {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
File f = new File("print.txt");
System.out.println("'y' for yes or 'n' for no");
try {
BufferedWriter k = new BufferedWriter(new FileWriter(f));
if (s.next().equals("y")) {
k.write("y");
}
else {
k.write("n");
}
k.flush();
}
catch (Exception e){
e.printStackTrace();
}
}
}
I am on Windows 10 and the commands I'm using to execute the program are 我在Windows 10上,用于执行程序的命令是
javac "C:\Users\David\Documents\Java\test.java"
java -cp "C:\Users\David\Documents\Java" test
Thank you for any help. 感谢您的任何帮助。
Nevermind, I realized that the file was just being created in a different directory since I didn't specify a directory to write the file to in the code. 没关系,我意识到该文件只是在另一个目录中创建的,因为我没有在代码中指定要写入文件的目录。 Silly me.
傻我
yeah.. you got it. 是的..你明白了。 While using cp- (ClassPath cmd) you must specify the name of directory where that java file needs to get saved.
使用cp-(ClassPath cmd)时,必须指定需要保存该Java文件的目录名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.