我的编码出了什么问题? 我试图创建一个文件名&打印输出在文本文件中 包实用12;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class q4 {
public static void main (String []args) throws FileNotFoundException{
System.out.println("please input file name");
String name;
//take file name
Scanner in;
in = new Scanner(System.in);
name = in.nextLine();
PrintWriter out = new PrintWriter(name);
// create textfile
out.println("Hello! My first program in File");
// write the text
out.close();
//close the file
}
}
答案 0 :(得分:1)
如果您刚刚输入了文件名而不是绝对路径,那么它将在您程序的当前工作目录中创建。要使用java检查当前工作目录,请检查Getting the Current Working Directory in Java
答案 1 :(得分:-2)
当你给程序提供文件名时,最后必须有.txt或其他东西。如果没有fileWriter则无法创建文件并抛出错误。如果你想确保程序点崩溃就是一个例子。
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class q4 {
public static void main (String []args) throws FileNotFoundException{
System.out.println("please input file name");
String name;
//take file name
Scanner in;
in = new Scanner(System.in);
name = in.nextLine();
PrintWriter out = new PrintWriter(name+".txt");
// create textfile
out.println("Hello! My first program in File");
// write the text
out.close();
//close the file
}
}