簡體   English   中英

寫入時FileOutputStream(File,append)不追加

[英]FileOutputStream(File, append) does not append when writing

此代碼是對象的一部分,每個對象都應將其位置寫入accounttorage.txt,但即使我將append設置為true,它也只寫入第一個對象的位置

accountstorage = new File(currDir + "/Clients/accountstorage.txt");

        try {
            if(!accountstorage.exists()) {
        accountstorage.getParentFile().mkdirs();
        accountstorage.createNewFile();
            } else {
                return;
            }


            fos = new FileOutputStream(accountstorage, true);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));


        bw.write("@" + accountfile.getParentFile() + "\r\n");
        bw.flush();
        bw.close();

是什么原因造成的? 我似乎找不到自己的問題。

由於您的條件障礙

 if(!accountstorage.exists()) {
    accountstorage.getParentFile().mkdirs();
    accountstorage.createNewFile();
} else {
    return; // DUE to this... Remove else block to fix.
}

如果文件不存在,它將創建該文件並寫入位置,但是下次文件存在時,它將返回並且不寫入任何內容。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM