java - ReadFile 无法解析为类型

标签 java readfile

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

我真的很想了解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/

相关文章:

java - JFrame 中的 NullPointerException

Java Swing Web 浏览器如何注意到 URL 不存在?

php - PHP 的 Readfile 方法的安全问题

c - 同步ReadFile读取的字节数

java - 在java上读取.dat文件获取null

java - 当第一个空白出现时如何读取文件

java - 确保该方法返回一个 List<String> 而不仅仅是一个 List。在java中

java - 在类路径上发现多个绑定(bind) & 在类路径上检测到 log4j-over-slf4j.jar 和 slf4j-log4j12.jar

java - Spring Boot JMS - 何时应在 @Transacted 方法上发送消息?

PHP 强制下载 .xlsx 文件损坏