简体   繁体   中英

BufferedWriter java problems

i try to print something to file. so i create a array of BufferedWriters (there is reason why array) . and when i run the program, nothing happend. the files are empty.

here is my code:

BW = new BufferedWriter[8];
for(int  i = 0; i < 8; i++){
    BW[i] = new BufferedWriter(new FileWriter(TablePath + i + ".txt"));
    BW[i].write("asdfgh");

}

this code create the txt files. but not write then anything.

what is the problem?

Close the BufferedWriters. Doing so will also flush their content so you don't have to call flush() as well.

Like this:

for (int i = 0; i < 8; i++) {  
    BW[i].close();
}

Add this line inside loop

BW[i].Close();

It will work fine after that, you have to flush and close the BW.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM