我有两个文件。一个文件计算我在文本文件中列出的事件数,并将事件数存储到变量“count”中。然后我想使用这个变量中的值在第二个文件中进行计算。我该怎么做呢?我是否必须在我的第一个文件中创建该类的对象然后引用它?我需要一个例子,我似乎无法让它工作。这是我尝试过的。
我的第一个文件:
import java.util.*;
import java.io.*;
public class EventCounter {
public static void main (String [] args) throws IOException{
Scanner file = new Scanner(new File("event.txt"));
int count = 0;
while (file.hasNextLine()) {
count++;
file.nextLine();
}
System.out.println(count); //test
}
}
我的第二个文件:
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class ReadEventFile {
private String path;
public ReadEventFile(String file) {
path = file;
}
public String[] OpenFile() throws IOException {
FileReader fr = new FileReader(path);
BufferedReader textReader = new BufferedReader(fr);
EventCounter method = new EventCounter(); //make object?
String[] dataTable = new String[count];
int i;
for (i=0; i<count; i++) { //Why count does not exist?
}
我的第二个文件不知道 count 是我第一个文件中的一个变量:-(