0

我正在将数据写入.dat文件。数据包含18000 tests每分钟运行一次,文件包含 24 小时的所有这些测试,并创建一个新文件。

除了该.dat文件,我还必须创建一个.idx文件,该文件将取出每个我们开始时的数据并打印并保存它。

我不确定是否可以在一个类中创建.dat文件和.idx文件,还是必须为每个文件创建 2 个单独的类?

4

3 回答 3

3
class FileWriterExample {
    FileWriter writer1 = new FileWriter(new File(path1));
    FileWriter writer2 = new FileWriter(new File(path2));

    //You can write to any of those indiferently
    //just remember to close them
    try {

    }finally{
        if(writer1 != null){
            writer1.flush();
            writer1.close();
        }
        if(writer2 != null){
            writer2.flush();
            writer2.close();
        }
    }
}
于 2013-11-07T10:32:17.867 回答
0

我承认我并不完全理解您要解决的问题,但是:

  • 您当然可以从同一个类中写入两个单独的文件。
  • 如果您的功能指示具有特定职责的类,那么可能值得将它们分开(并将文件处理放在相关类中)。例如,您似乎有一个索引器,它很可能是一个单独的实例,它采用原始数据来生成和写入索引。
  • 事实上,你甚至可能有一个DataProducer和一个DataIndexer类来产生相关的输出和一个DataWriter管理文件输出的类。
  • 最后,如果这是一项大型且耗时的活动,则将“繁重的工作”分成更小的单元将允许其分布(例如在网格、空间、云中)以促进并行处理。索引器线程可以单独聚集。

综上所述,我同意@SirDarius 的评论并首先进行,然后重构以隔离核心工作以使其高效。

于 2013-11-07T10:39:22.813 回答
0

I am unsure as to whether I can create the .dat file and .idx file in the one class or do I have to create 2 separate classes for each file?

This seems to be a common thing people are unsure about, surprisingly. But a class is just a vehicle to think about programming tasks in a particulary way, you can assume that it only exists in your head. Just as you can use multiple strings, numbers and whatever in your class, so you can use as many files as you want.

Also, think of it like this: How should a file "know" from which class it is used? And why, even if this was possible, should someone set up things in such a way that it is impossible to use 2 files from the same class?

于 2013-11-07T10:40:39.430 回答