未找到Scanner类java文件 [英] Scanner class java file not found

查看:123
本文介绍了未找到Scanner类java文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

扫描程序类无法找到文件
我使用NetBeansIDE,而test.txt位于文件夹路径中:D:\ netbeans project works \ReadFile \src \readfile \ test.txt

Scanner Class couldnt find the file I use NetBeansIDE, and the test.txt is in the folder path: D:\netbeans project works\ReadFile\src\readfile\test.txt

在readfile.java存在的同一文件夹中。
代码如下。
生成未找到的文件。

in the same folder the readfile.java exsist. the code is as below. It generates file not found.

package readfile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;


public class ReadFile {

    public static void main(String[] args) throws IOException , FileNotFoundException 
    {  
        Scanner scanner = new Scanner(new File("test.txt"));  

        while (scanner.hasNextLine())  
            System.out.println(scanner.nextLine());  
    }  
}

输出: -

run:
Exception in thread "main" java.io.FileNotFoundException: test.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:106)
    at java.util.Scanner.<init>(Scanner.java:636)
    at readfile.ReadFile.main(ReadFile.java:14)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)


推荐答案

在创建扫描仪 class:

System.out.println(new File("test.txt").getAbsolutePath());

它将显示JVM期望找到该文件的位置以及它是否是您期望的文件夹。

It will show you where JVM expects to find the file and whether it is the folder you expect as well.

同时检查文件权限。但很可能这是默认JVM目录的问题。

Also check file permissions. But most likely it is a problem with default JVM directory.

这篇关于未找到Scanner类java文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆