找不到另一个类文件的符号

发布于 2024-12-04 10:44:39 字数 2253 浏览 3 评论 0 原文

我已经遇到过这个问题几次,我创建了另一个类文件,但主类文件找不到它。 这是主类文件:

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 作为 这个。那是行不通的。我已确保已编译 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 错误。 提前致谢!

I've had this problem a few times, where I've created another class file and the main class file can't find it.
Here's the main class file:

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

Here is the class file it can't find:

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;
}
  }

I've tried running javac textfiles\ReadFile.java and javac textfiles\FileData.java as a suggestion for this. That doesn't work. I've made sure I have compiled ReadFile and fixed all the errors there.
The compiler error I get is:

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

I'm using notepad++and .cmd so it can't be an IDE error.
Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青朷 2024-12-11 10:44:39

确保 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 目录。

Make sure the java files are all in the textfiles directory:

textfiles/FileData.java
textfiles/ReadFile.java

And run:

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

Your code works without any modification. I think you are compiling from a wrong directory:

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

Move the FileData.java to the textfiles directory.

痴者 2024-12-11 10:44:39

您必须编译主类使用的所有 java 文件。由于 FileData 使用 ReadFile,因此您也必须编译它。

你尝试过

javac Filedata.java ReadFile.java

还是

javac *.java

You have to compile all the java files used by your main class. As ReadFile is used by FileData you have to compile it too.

Did you tried

javac Filedata.java ReadFile.java

or

javac *.java

?

梦中的蝴蝶 2024-12-11 10:44:39

肯定和生成的类有冲突。
只需尝试删除所有已生成的类并再次构建项目即可。

There must be a conflict with generated classes.
Just try to remove all the classes that have been generated and build project again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文