我有兩個文件。一個文件計算我在文本文件中列出的事件數量,並將事件數量存儲到變量「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?
}
我的第二個文件不知道那算不算是我的第一個文件的變量:-(
愚人節.......... – Zelldon