我正在尝试按照一段代码打开并读取文本文件。为此,我有一个名为 readText 的包。在内部,我构建了一个类 readLocalFile 来打开和读取文件,并构建了一个 main 方法来调用它。下面是这两个类。
public class readFileLocal {
private String path;
public readFileLocal(String file_path){
path = file_path;
}
int readLines() throws IOException{
FileReader file_to_read = new FileReader(path);
BufferedReader lines = new BufferedReader (file_to_read);
int numberOfLines = 0;
while(lines.readLine()!= null) {
numberOfLines ++;
}
lines.close();
return numberOfLines;
}
public String[] openFile() throws IOException{
FileReader freader = new FileReader (path);
BufferedReader textReader = new BufferedReader (freader);
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
int i; /* put all the lines of text from the file to the array*/
for (i=0; i<numberOfLines; i++){
textData[i] = textReader.readLine();
}
textReader.close();
return textData;
}
}
然后我有一个主类来调用它。代码如下:
public class fileData {
public static void main(String[] args) throws IOException{
String file_name = "F:/Testfile.exl";
try{
readFileLocal file = new readFileLocal(file_name);
String[] arylines = file.openFile();
int i;
for (i=0; i<arylines.length; i++){
System.out.println(arylines[i]);
}
}
catch(IOException e) {
System.out.println(e.getMessage());
}
}
}
当我运行它时,Eclipse 给了我这个错误消息:
错误:在类 readText.fileData 中找不到 Main 方法,请将 main 方法定义为:public static void main(String[] args) 或 JavaFX 应用程序类必须扩展 javafx.application.Application
知道出了什么问题吗?
最佳答案
您可能应该开始一个新项目。 Eclipse 认为您正在运行 JavaFX 程序(在这种情况下 filedata 应该扩展 Application)。
只需在没有 JavaFX 的情况下进行正常构建即可。
关于Java拒绝public static void main方法,请求public static void main方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29001035/