gpt4 book ai didi

java - ReadFile 无法解析为类型

转载 作者:行者123 更新时间:2023-12-01 13:07:14 28 4
gpt4 key购买 nike

我被认为是一个菜鸟,我在互联网上搜索了几个小时来解决我的问题,但仍然没有运气。

我真的很想了解java,如果你能解释一些细节,我将非常感激。

问题出在这一行

ReadFile file = ReadFile(file_name);

error message : "ReadFile cannot be resolved to a type."

这是我的代码:FileData.java

package textfiles;
import java.io.IOException;

public class FileData {

public static void main (String[] args) throws IOException {

String file_name = "D:/java/readfile/test.txt";

try {

ReadFile file = ReadFile(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() );
}

}
}

这是我的其他代码:ReadFile.java

package textfiles;

import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;

public class ReadFile {

private String path;

public ReadFile (String file_path) {

path = file_path;

}
int readLines () throws IOException{

FileReader file_to_read = new FileReader(path);
BufferedReader bf = new BufferedReader(file_to_read);

String aLine;
int numberOfLines = 0;

while (( aLine = bf.readLine() ) != null) {

numberOfLines++;

}

bf.close();

return numberOfLines;
}
public String[] OpenFile () throws IOException {

FileReader fr = new FileReader (path);
BufferedReader textReader = new BufferedReader (fr);


int numberOfLines = readLines();
String[] textData = new String[numberOfLines];

int i;

for (i=0; i < numberOfLines; i++) {

textData[i] =textReader.readLine();
}


textReader.close();
return textData;
}
}

最佳答案

试试这个:

ReadFile file = new ReadFile(file_name);

为了使用类名初始化对象,您应该使用 new 关键字,如下所示:

ClassName objName = new ClassName(arguments);

从代码的其余部分来看,您似乎知道这个概念,但我建议您(或 future 可能的访问者)访问 this页。

关于java - ReadFile 无法解析为类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23179623/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com