【问题标题】:FileReader cannot find fileFileReader 找不到文件
【发布时间】:2021-08-30 22:04:16
【问题描述】:

我有一个文本文件,其中包含以下程序的字典。 由于某种原因,“Word.txt”甚至在 eclipse 上的文件扩展名上都找不到 我也试过在记事本上做,但我没有。希望有人能帮我解决这个问题。

Word.txt 文件包含:

algebra<===>a generalization of arithmetic in which letters representing numbers are combined according to the rules of arithmetic
book<===>a set of written sheets of skin or paper or tablets of wood or ivory
calculus<===>the mathematical methods comprising differential and integral calculus
data<===>factual information such as a basis for reasoning, discussion, or calculation
grammar<===>a system of rules that defines the grammatical structure of a language
information<===>knowledge obtained from investigation, study, or instruction
language<===>the words, their pronunciation, and the methods of combining them used and understood by a community
laptop<===>a portable microcomputer having its main components (such as processor keyboard, and display screen) integrated into a single unit capable of battery-powered operation
metadata<===>data that provides information about other data

错误是:

Exception in thread "main" java.io.FileNotFoundException: Word.txt 
   (The system cannot find the file specified)

    at java.base/java.io.FileInputStream.open0(Native Method)
    at java.base/java.io.FileInputStream.open(FileInputStream.java:212)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:154)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:109)
    at java.base/java.io.FileReader.<init>(FileReader.java:60)
    at PL1.main(PL1.java:18)

代码:

import java.io.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class PL1 {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner input = new Scanner(System.in);
        File file = new File("Word.txt");
        FileReader R = new FileReader("Word.txt");
        BufferedReader Br = new BufferedReader(R);
        BufferedReader In = null;
        In = new BufferedReader(new FileReader("Word.txt"));
        System.out.println(new File(".").getAbsoluteFile());
        int choices;
        int number;
        int x = 1;
        String s = "";
        String read = null;

        try {
            for (int i = 0; i <= 10; i++) {
                System.out.println("--------------SELECT FROM THE CHOICES--------------");
                System.out.println("1 - Show all records");
                System.out.println("2 - Search for definition");
                System.out.println("3 - Add record");
                System.out.println("4 - Delete record");
                System.out.println("5 - Delete all records");
                System.out.println("6 - Exit");
                System.out.println("---------------------------------------------------");
                choices = input.nextInt();
                switch (choices) {
                    case 1: 
                        x = 1;
                        System.out.println("--------------------ALL RECORDS--------------------");

                        try {
                            while ((read = In.readLine()) != null) {
                                String[] splitted = read.split("<===>");
                                for (String part : splitted) {
                                    System.out.print(x++ + ". " + part + "\t \n");
                                }
                            }

                            System.out.println("< Records found: " + x / 2 + " >");

                        } catch (IOException e) {
                            System.out.println("There was a problem: " + e);
                            e.printStackTrace();
                        }
                        break;

                    case 2:
                        String searchword;
                        System.out.println("-------------------SEARCH RECORD-------------------");
                        System.out.println("Word:");
                        searchword = input.nextLine();

                        try {
                            while ((s = Br.readLine()) != null) {
                                String[] splitted = s.split("<===>");
                                number = splitted.length;
                                for (int e = 0; e < splitted.length; e += 2) {
                                    for (int j = 1; j < splitted.length; j += 2) {
                                        if (splitted[e].equals(searchword)) {
                                            splitted[e] = splitted[j];
                                            System.out.println("-" + splitted[e]);
                                        }
                                    }
                                }
                            }
                        } catch (IOException e) {
                            System.out.println("There was a problem: " + e);
                            e.printStackTrace();
                        }
                        break;

                    case 3:
                        String word;
                        String definition;
                        System.out.println("--------------------ADD RECORD--------------------");
                        System.out.println("Word:");
                        word = input.nextLine();
                        word = input.nextLine();
                        System.out.println("Definition:");
                        definition = input.nextLine();

                        FileWriter fw = new FileWriter("Word.txt", true);
                        PrintWriter out = new PrintWriter(fw);
                        out.print(" \n" + word + "<===>" + definition);
                        out.close();
                        System.out.println("< Record has been saved successfully. >");
                        break;

                    case 6:
                        System.out.println("Thank You!");
                        return;

                    default:
                        System.out.println("Invalid Input!");
                        break;
                }
            } 
        } catch (Exception e) {
            System.out.println("Invalid Input!");
            return;
        }
    }
}

【问题讨论】:

  • 文件Word.txt的完整路径是什么?
  • 我把它放在桌面和资源上
  • 我没有问你把文件放在哪里,我问的是完整路径是什么。您是说在两个不同的文件夹中有两个名为Word.txt 的文件吗? Desktop 文件夹的路径是 C:\Users\USER\Desktop,其中 USER 是您的用户名。假设您的用户名为Francis,则路径为C:\Users\Francis\Desktop,这意味着文件的路径为C:\Users\Francis\Desktop\Word.txt。对吗?

标签: java eclipse file filereader filewriter


【解决方案1】:

这是因为您没有为程序提供此“Word.txt”的完整路径,即:假设文件在您的桌面上,那么您应该执行以下操作:

FileReader R = new FileReader("C\YourUser\Desktop\Word.txt");

【讨论】:

    【解决方案2】:

    所以你的主要问题是你的路径。 如果Word.txt 在 src 文件夹中,那么你必须说喜欢它:- FileReader reader = new FileReader("src\\if any another folder\\Word.txt"); 如果它只是在 src 文件夹之外,那么您可以使用 Word.txt 作为路径 如果路径完全不同,则在此处写完整路径

    【讨论】:

      【解决方案3】:

      请检查您的文件路径。如果您的文件在项目的任何包(如 src 或类似的东西)内或在项目文件夹之外,则将引发 IOException。在这种情况下,您需要提供 FileReader 的完整路径

      FileReader fr = new FileReader("C:/user/demo-project/src/word.txt")
      

      FileReader fr = new FileReader("C:/user/desktop/word.txt")
      

      如果您不想传递完整的文件路径,则必须将文件直接放在项目文件夹中。

      参考下图链接: FileReader

      【讨论】:

        猜你喜欢
        • 2014-03-05
        • 2023-03-08
        • 2014-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-15
        • 1970-01-01
        相关资源
        最近更新 更多