简体   繁体   中英

Having trouble writing to a file

I have a Gui for a stopwatch, it has a Start button, a Stop button, and also a "Split" button, and a Save Splits button. The stopwatch records splits and I would like to be able to write them to a file but I have an error with:

FileWriter splitsWriter= new FileWriter("a.txt");
for(int i=0;i<theSplits.size();i++){
    splitsWriter.write(theSplits.get(i));
}

It says Unhandled exception type IOException but I thought a writer creates the file if it doesn't exist so why should this exception be a problem? I'm just confused..

Like pstrjds already said you have to add a try/catch block. Your code should look like this:

try {
    FileWriter splitsWriter= new FileWriter("a.txt");
    for(int i=0;i<theSplits.size();i++){
        splitsWriter.write(theSplits.get(i));
    }
} catch (IOException e) {
    // Do something to handle the exception
}

This should compile.

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