【发布时间】: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