而不是有15个BufferedWriters,有没有一种方法,我可以写15个不同的文件使用数组?
public static void main (String [] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("" ));
BufferedWriter bw = new BufferedWriter(new FileWriter( "1occurrence.txt"));
HashMap<String,Integer> hash = new HashMap<String,Integer>();
while ((line= br.readLine() ) != null) {
if ( !hash.containsKey(genesym)) {
hash.put (genesym, 1);
int count=0;
bw.write(line + "n");
}
else {
count = hash.get(genesym);
if (count==1) {
bw2.write(line + "n");
count++;
}
else if (count==2) {
bw3.write(line + "n");
count++;
}
然后我有12个else if语句要写到最多15个不同的文件。比如
BufferedWriter [] bw = new BufferedWriter[15];
for (int i=0; i<15; i++) {
array[i] = new BufferedWriter(); }
我意识到这个语法非常不正确,但这是我能解释它的最好方式。
谢谢你的帮助!
我不明白为什么需要创建BufferedWriter
数组。
如果您的要求是写入15个不同的文件,那么您可以使用包含15个文件名的数组,并在每次迭代中更改BufferedWriter
的引用:
String[] fileNames = {"file1.txt","file2.txt",....} // 15 file names;
BufferedWriter writer = null;
for(int i=0;i<fileNames.length;i++)
{
writer = new BufferedWriter(new FileWriter(fileNames[i]));
writer.wrtier(data); // write data to file
}