有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

资源Java找不到文件?

嗨,我正在设置一个扫描仪来打印文本文件的内容。这是我的代码:

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

public class ScannerReadFile {

    public static void main(String[] args) {

        // Location of file to read
        File file = new File("CardNative.java.txt");

        try 
        {

                Scanner scanner = new Scanner(file);

                while (scanner.hasNextLine()) 
                {
                    String line = scanner.nextLine();
                    System.out.println(line);
                }
                scanner.close();
        } 
        catch (FileNotFoundException e) 
        {
              e.printStackTrace();
        }

    }
}

我在项目中创建了一个源文件夹,并将文本文件放在其中。然而,我不断地发现这个错误:

java.io.FileNotFoundException: CardNative.java.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:120)
    at java.util.Scanner.<init>(Scanner.java:636)
    at ScannerReadFile.main(ScannerReadFile.java:14)

共 (5) 个答案

  1. # 1 楼答案

    new File(String pathname);
    

    您正在使用的coinstructor将filepath作为参数,可以是绝对参数,也可以是相对参数。如果它是相对的,它将是执行路径/您的_字符串。 所以你应该把文件放在与编译文件相同的文件夹中。jar文件

    File file1 = new File("text.txt");
    File file2 = new File("D:/documents/test.txt");
    

    如果程序是从C:/programms/myprj执行的。罐子,所以 file1将打开“C:/programms/test.txt”,file2将独立于执行路径打开“D:/documents/test.txt”

    http://docs.oracle.com/javase/7/docs/api/java/io/File.html#File(java.lang.String)

  2. # 2 楼答案

    这应该行得通

    public static void main(String[] args) {
    
    
                Scanner scanner = new Scanner(ScannerReadFile.class.getResourceAsStream("CardNative.java.txt"));
    
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    System.out.println(line);
                }
                scanner.close();
    
        }
    }
    
  3. # 3 楼答案

    您需要确保文件位于运行程序的同一目录中。尝试在主函数的顶部添加以下内容,以查看当前目录是什么,以及文件是否实际位于该目录中:

    系统。出来println(System.getProperty(“user.dir”)

  4. # 4 楼答案

    我在评论中发布了我的答案,但我不允许发表评论,因为我没有足够的声誉。与您的评论一样,您在文件路径中使用了反斜杠。而是使用双反斜杠\或一个正斜杠/。例如C:/java/file。txt 您应该为它提供文件的正确和实际路径,或者确保文件位于源所在的位置

    public class ScannerReadFile {
    
        public static void main(String[] args) {
    
            // Location of file to read
            File file = new File("C:/Users/EastCorporation/Desktop/CardNative.java.txt");
    
            try {
    
                Scanner scanner = new Scanner(file);
    
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    System.out.println(line);
                }
                scanner.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
    
        }
    }
    
  5. # 5 楼答案

    您可以使用System.out.println(System.getProperty("user.dir"));查看Java在默认情况下查找文件的文件夹
    如果文件不存在,则必须指定文件的整个路径