我正在尝试从文件中读取一个单词并将其显示在控制台中。尽管文件包含文本,但结果始终是 null
。代码中可能存在什么错误?
这是代码:
public class ParsingStrings{
private Scanner sc;
private static String input;
public void openFile(){
try{
sc = new Scanner(new File ("E:\\Dropbox\\File.txt"));
input = sc.next();
}catch(FileNotFoundException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println(input);
}
}
最佳答案
您应该在将输入打印到控制台之前调用 openFile - 方法。
关于java - 生成 null(从文件中读取一个单词),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15588383/