找不到其他类文件的符号

问题描述 投票:2回答:3

我几次遇到这个问题,在这里创建了另一个类文件,而主类文件找不到它。这是主类文件:

package textfiles;

import java.io.IOException;
 public class FileData
 {

public static void main(String[] args)
{
    String file_name = "Lines.txt";

    try {
        ReadFile file = new ReadFile(file_name);
        String[] aryLines = file.OpenFile();

        for(int i =0; i<aryLines.length; i++)
        {
            System.out.println(aryLines);
        }
    }

    catch(IOException e)
    {   
        System.out.println(e.getMessage());
    }
}
  }

这里是找不到的类文件:

package textfiles;

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

 public class ReadFile
 {
private String path;
int numberOfLines=0;

public ReadFile(String file_path)
{
    path = file_path;
}

public String[] OpenFile() throws IOException
{
    FileReader fr = new FileReader(path);
    BufferedReader br = new BufferedReader(fr);

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

    for(int i=0; i<numberOfLines; i++)
    {
        textData[i] = br.readLine();
    }

    br.close();
    return textData;
}

int readLines() throws IOException
{
    FileReader file_to_read = new FileReader(path);
    BufferedReader bf = new BufferedReader(file_to_read);

    String aLine;

    while((aLine = bf.readLine()) != null)
    {
        numberOfLines++;
    }

    bf.close();
    return numberOfLines;
}
  }

我尝试运行javac textfiles \ ReadFile.java和javac textfiles \ FileData.java作为this的建议。那不行我确保已编译ReadFile并修复了那里的所有错误。我得到的编译器错误是:

C:\Users\Liloka\Source>javac FileData.java
FileData.java:13: cannot find symbol
symbol  : class ReadFile
location: class textfiles.FileData
                    ReadFile file = new ReadFile(file_name);
                    ^
  FileData.java:13: cannot find symbol
  symbol  : class ReadFile
  location: class textfiles.FileData
                    ReadFile file = new ReadFile(file_name);
                                        ^
  2 errors

我使用的是notepad ++和.cmd,因此不会出现IDE错误。提前致谢!

file class find symbols
3个回答
9
投票

确保Java文件都在textfiles目录中:

textfiles/FileData.java
textfiles/ReadFile.java

并运行:

javac textfiles/FileData.java textfiles/ReadFile.java 
java textfiles.FileData

您的代码无需任何修改即可工作。我认为您是从错误的目录进行编译的:

C:\ Users \ Liloka \ Source> javac FileData.java

FileData.java移动到textfiles目录。


4
投票

您必须编译主类使用的所有Java文件。由于FileData使用ReadFile,因此您也必须对其进行编译。

您尝试过吗

javac Filedata.java ReadFile.java

javac *.java


0
投票

与生成的类必须存在冲突。只需尝试删除所有已生成的类并再次构建项目即可。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.