定义了一个读取文件 的类ReadTxtFile,主类是Read,在主程序中引用类ReadTxtFile,但是说类的方法未定义 ,怎么解决?
下面这个是ReadTxtFile的定义 :
package test;
import java.io.*;
public class ReadTxtFile {
public readTxt(String filePath) {
try {
File file = new File(filePath);
if(file.isFile() && file.exists()) {
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
BufferedReader br = new BufferedReader(isr);
String lineTxt = null;
while ((lineTxt = br.readLine()) != null) {
System.out.println(lineTxt);
}
br.close();
} else {
System.out.println("no exit");
}
} catch (Exception e) {
System.out.println("wrong doing");
}
}
}
下面是主程序:
```java
package test;
import test.ReadTxtFile;
public class Read {
public static void main(String[] args) {
String filePath = "F:\\javawork\\readtxtfile\\mdata.txt";
ReadTxtFile f = new ReadTxtFile();
f.readTxt(filePath);
}
}
下面是运行结果:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method readTxt(String) is undefined for the type ReadTxtFile
at test/test.Read.main(Read.java:7)
求解: