我正在将数据写入.dat
文件。数据包含18000 tests
每分钟运行一次,文件包含 24 小时的所有这些测试,并创建一个新文件。
除了该.dat
文件,我还必须创建一个.idx
文件,该文件将取出每个我们开始时的数据并打印并保存它。
我不确定是否可以在一个类中创建.dat
文件和.idx
文件,还是必须为每个文件创建 2 个单独的类?
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();
}
}
}
我承认我并不完全理解您要解决的问题,但是:
DataProducer
和一个DataIndexer
类来产生相关的输出和一个DataWriter
管理文件输出的类。综上所述,我同意@SirDarius 的评论并首先进行,然后重构以隔离核心工作以使其高效。
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?