为什么 createNewFile() 不能识别它的导入?
Why won't createNewFile() recognize it's import?
我在 NetBeans 项目中创建了这个 class,但无法弄清楚为什么 createNewFile 方法无法识别它的导入。 NetBeans 给出了该行的 "cannot find symbol" 错误。 "createNewFile" 是该行中唯一用红色下划线标出的部分。它还在 "import java.io.File" 上发出警告,说它从未被使用过。
我在该方法周围添加了 try 和 catch 块,但它们没有任何区别。为了简单起见,在下面的示例中去掉了它们。
import java.util.Scanner;
import java.io.File;
public class Bleh {
Scanner in = new Scanner(System.in);
User u = new User();
public void setUserName() {
System.out.print("Name: ");
u.setName(in.nextLine());
}
public void checkForAccount() {
createNewFile(u.getName());
}
}
您对 java.io.File
的导入将 恰好 class 本身导入您的命名空间。它并没有导入File
的所有方法,即使导入了,也没有这样的方法File.createNewFile(String)
;您需要创建一个 File
对象并调用其方法:
new File(u.getName()).createNewFile();
我在 NetBeans 项目中创建了这个 class,但无法弄清楚为什么 createNewFile 方法无法识别它的导入。 NetBeans 给出了该行的 "cannot find symbol" 错误。 "createNewFile" 是该行中唯一用红色下划线标出的部分。它还在 "import java.io.File" 上发出警告,说它从未被使用过。
我在该方法周围添加了 try 和 catch 块,但它们没有任何区别。为了简单起见,在下面的示例中去掉了它们。
import java.util.Scanner;
import java.io.File;
public class Bleh {
Scanner in = new Scanner(System.in);
User u = new User();
public void setUserName() {
System.out.print("Name: ");
u.setName(in.nextLine());
}
public void checkForAccount() {
createNewFile(u.getName());
}
}
您对 java.io.File
的导入将 恰好 class 本身导入您的命名空间。它并没有导入File
的所有方法,即使导入了,也没有这样的方法File.createNewFile(String)
;您需要创建一个 File
对象并调用其方法:
new File(u.getName()).createNewFile();