0
而不是有15個BufferedWriters,有沒有一種方法可以使用數組寫入15個不同的文件?Java:使用BufferedWriter數組寫入多個文件(或其他方式?)
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(); }
我意識到這句法是非常不正確,但是這是我可以解釋它的最佳方式。
感謝您的幫助!
聽起來像你想要的是一個MultiWriter。看看這個答案:https://stackoverflow.com/questions/25382876/java-make-a-printwriter-write-two-different-writers –