簡體   English   中英

我無法將內容寫入自制的txtfile

[英]I can't write stuff to my self-made txtfile

我第一次嘗試將自己的txt寫到特定目錄中的aa文件中。 test.mod文件放置正確,但是當我打開該文件時它是空的並且不包含任何文本。 我在這里想念什么?

   public static void main(String[] args) {
    String pad = "C:\\Users\\Bernard\\Documents\\Paradox Interactive";
    File bestand = new File(pad + "\\test.mod");
    try {
        BufferedWriter pen = new BufferedWriter(new FileWriter(bestand));
        pen.write("line1");
        pen.write("line2");

    }catch(IOException e){            
    }
}

感謝您的時間

當您寫入BufferedWriter您(潛在地)正在寫入內存中的緩沖區,並且必須flush()寫入以確保它們到達磁盤。 close()還會在任何合理的實現上隱式調用flush() ,但是依賴於它並不是一個好習慣:

public static void main(String[] args) {
    String pad = "C:\\Users\\Bernard\\Documents\\Paradox Interactive";
    File bestand = new File(pad + "\\test.mod");
    BufferedWriter pen = null;
    try {
        pen = new BufferedWriter(new FileWriter(bestand));
        pen.write("line1");
        pen.write("line2");
        pen.flush();
    }catch(IOException e){
        // Probably should have some treatment here too
    }
    finally {
        if (pen != null) {
            pen.close();
        }
    }
}

暫無
暫無

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

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