简体   繁体   中英

FileOutputStream wait for finishing writing

I have many byte[] pieces to write to a file and these pieces are kinda big and I think the bytes are getting mixed...

Is there any methods to wait for .write(byte[]) to finish his work or other ways to deal with this problem? When I watch the file the first half is ok , then when I start writing the second half, some time it ends with the first half.... The byte array is good, I tested them. If you need any explain of code feel free ask.

try (FileOutputStream fos = new FileOutputStream(Main.newFile)) {
        for (int y = 0; y < piecesCount; y++) {
            if (this.myList[y] == 1) {
                fos.write(this.piecesArray.get(y).piece);
                fos.flush();
                System.out.println("Im writing " + y + " piece");
            } else {
                y--;
            }
        }
        fos.close();
    } catch (FileNotFoundException ex) {
    } catch (IOException | InterruptedException ex) {
    }

Well, if piecesCount is a long the int y variable may overflow and start wrtiting the first part of the array.

If this is not the case, I don't see other bugs in this code that may cause what you described. In which case there must be something going on in

piecesArray.get(y).piece

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