2016-07-17 7 views
0
FileWriter fwriter = new FileWriter("C:/Users/NULL NULL NULL/Desktop/New Text Document.txt", false); // set the write to false so we dont append to it 

BufferedWriter write = new BufferedWriter(fwriter); // this is the type so we can write to it. 

write.write(""); // write empty string to delet everything 

write.close(); // close the buffer to gain back memory space that the buffer 

が取り上げました。javaでファイルを削除せずにファイルの内容を空にするには?

このことについてどのように
+0

まずは何が問題なのですか?次に指摘したコードをテストしましたか? –

+8

私はこれがこれと重複していると信じています:[http://stackoverflow.com/questions/6994518/how-to-delete-the-content-of-text-file-without-deleting-itself](http://stackoverflow .com/questions/6994518 /テキストファイルの内容を削除する方法自体を削除する) –

答えて

2

:新しいファイルを作成するか、既存のものを上書きします

File file = new File("myfile"); 
if(file.exists()){ 
    file.delete(); 
} 
file.createNewFile(); 

またはこの、:

Files.newBufferedWriter(Paths.get("myfile")); 

それともCandiedOrangeのPrinterWriter @:そうでなければ

new PrintWriter("myfile").close(); 

、良い古い方法:

File file = new File("myfile"); 
FileOutputStream fooStream = new FileOutputStream(file, false); 

fooStream.write("".getBytes()); 
fooStream.close(); 
+1

'new PrintWriter(" filepath.txt ")の何が問題なのですか?close();' ? – CandiedOrange

+0

何も間違っていない、それを行う方法は何百万もあります:)あなたの提案をミックスに加えました – alexbt

関連する問題