-1

我有两个文件。一个文件计算我在文本文件中列出的事件数,并将事件数存储到变量“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 是我第一个文件中的一个变量:-(

4

2 回答 2

1

你似乎有你的流程倒流。带有该main方法的类将由 JVM 创建和运行 - 因此它是您的入口点。

因此,您的ReadEventFile班级需要被告知count它的创建时间。只需将其添加到构造函数中:

public static class ReadEventFile {

    private final File eventFile;
    private final int count;

    public ReadEventFile(final int count, final File eventFile) {
        this.eventFile = eventFile;
        this.count = count;
    }

    public String[] openFile() throws IOException {
        String[] dataTable = new String[count];
        int i;
        for (i = 0; i < count; i++) {
        }
        return dataTable;
    }
}

现在你EventCounter需要创建一个ReadEventFile实例,一旦它知道count并调用openFile它的方法:

public static void main(String[] args) throws IOException {

    final File eventFile = new File("event.txt");
    int count = 0;
    try (Scanner file = new Scanner(eventFile)) {
        while (file.hasNextLine()) {
            count++;
            file.nextLine();
        }
    }
    final ReadEventFile readEventFile = new ReadEventFile(count, eventFile);
    final String[] dataTable = readEventFile.openFile();
}

ReadEventFile是否有效,然后返回String[]到您的EventCounter.

完成后,您不会关闭任何资源。这是自找麻烦。我在您ScannerEventCounter.

这个程序的设计确实有点奇怪。没有逻辑上的理由EventCounter应该是应用程序的入口点。我建议您创建一个BootStrap包含该main方法的类,并且是调用该方法的入口点EventCounterReadEventFile.

此外,该类openFile上的方法ReadEventFile没有很好地命名——它的作用不止于此。也许processEventFile或类似的东西会更合适。

于 2013-04-01T11:52:41.957 回答
0

你的第一个程序

package farzi;

import java.util.*;
import java.io.*;
    public class EventCounter {
        public static void main (String [] args) throws IOException
        {
            EventCounter object = new EventCounter();
            System.out.println(object.returnCount());
        }

        public int returnCount() throws FileNotFoundException
        {
            Scanner file = new Scanner(new File("event.txt"));
            int count = 0;
            while (file.hasNextLine()) {
                count++;
                file.nextLine();
            }
            System.out.println(count); //test
            return count;
        }
    }

你的第二个程序

package farzi;
import java.io.File;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;

public class ReadEventFile 
{

    private String path;
    public ReadEventFile(String file) 
    {
        String path = file;
    }

    public String[] OpenFile() throws IOException {

        EventCounter eventCounterObject = new EventCounter();
        int countLocal = eventCounterObject.returnCount();

        FileReader fr = new FileReader(path);
        BufferedReader textReader = new BufferedReader(fr); 

        EventCounter method = new EventCounter(); //make object?
        String[] dataTable = new String[countLocal];

        int i;
        String[] textData = null;
        for (i=0; i<countLocal; i++) { //Why count does not exist?

            textData[i] = textReader.readLine();
        }
        return textData;

    }

}
于 2013-04-01T11:45:01.960 回答