2016-10-11 8 views
-2

ファイルを消去または消去せずにファイルに行を書き込む方法を知りたい。どんなクラスとパッケージが必要ですか? FileOutputStreamとPrintStream(printlnを使用)とBufferedWriterとOutputStreamWriter(writeを使用)を試しましたが、ファイルから以前の内容が消去されています。前の内容を消去せずにtxtファイルに書き込む

あなたがwriteStringToFile org.apache.commons.io

公共の静的な無効writeStringToFile(ファイルのファイル、 文字列データ、 文字セットエンコーディング、 ブールAPPEND)からクラスのfileutils下の方法を使用することができます

try 
    { 
     File txt = new File("marcos.txt"); 
     BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out)); 

     writer.write(registro); 
     writer.newLine(); 
     writer.close();  

    } 
    catch(Exception e) 
    { 
     System.out.println("Error en la escritura al archivo."); 
    } 

答えて

0

throws IOException ファイルが存在しない場合は、そのファイルを作成するファイルに文字列を書き込みます。 パラメータ: file - 書き込むファイル data - ファイルに書き込むコンテンツ encoding - 使用するエンコーディング、nullはプラットフォームのデフォルトを意味します append - trueの場合、ファイルの最後に文字列が追加されます 例外: IOException - I/Oエラーの場合

関連する問題