我須要將文本重複添加到Java中的現有文件中。 我怎麼作? html
我只添加一些小細節: java
new FileWriter("outfilename", true)
2.nd參數(true)是一個稱爲appendable ( http://docs.oracle.com/javase/7/docs/api/java/lang/Appendable.html )的功能(或接口)。 它負責將某些內容添加到特定文件/流的末尾。 此接口從Java 1.5開始實現。 ) with this interface can be used for adding content 帶有此接口的每一個對象(即 )均可以用於添加內容 apache
換句話說,您能夠將一些內容添加到壓縮文件中,或添加一些http進程 api
String str; String path = "C:/Users/...the path..../iin.txt"; // you can input also..i created this way :P BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new FileWriter(path, true)); try { while(true) { System.out.println("Enter the text : "); str = br.readLine(); if(str.equalsIgnoreCase("exit")) break; else pw.println(str); } } catch (Exception e) { //oh noes! } finally { pw.close(); }
這將知足您的預期。 oracle
這裏不是全部關於try / catch塊的答案都將.close()包含在finally塊中嗎? app
標記答案的示例: 函數
PrintWriter out = null; try { out = new PrintWriter(new BufferedWriter(new FileWriter("writePath", true))); out.println("the text"); } catch (IOException e) { System.err.println(e); } finally { if (out != null) { out.close(); } }
另外,從Java 7開始,您可使用try-with-resources語句 。 不須要finally塊來關閉已聲明的資源,由於它是自動處理的,而且也不太冗長: this
try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("writePath", true)))) { out.println("the text"); } catch (IOException e) { System.err.println(e); }
您是否出於記錄目的這樣作? 若是是這樣的話,有幾個庫 。 最受歡迎的兩個是Log4j和Logback 。 spa
若是您只須要執行一次,則使用Files類很容易: code
try { Files.write(Paths.get("myfile.txt"), "the text".getBytes(), StandardOpenOption.APPEND); }catch (IOException e) { //exception handling left as an exercise for the reader }
注意 :若是文件不存在,上述方法將拋出NoSuchFileException
。 它還不會自動追加換行符(追加到文本文件時一般會須要此換行符)。 史蒂夫·錢伯斯(Steve Chambers)的答案涵蓋了如何使用Files
類進行此操做。
可是,若是您要屢次寫入同一文件,則上述操做必須屢次打開和關閉磁盤上的文件,這是一個緩慢的操做。 在這種狀況下,使用緩衝寫入器更好:
try(FileWriter fw = new FileWriter("myfile.txt", true); BufferedWriter bw = new BufferedWriter(fw); PrintWriter out = new PrintWriter(bw)) { out.println("the text"); //more code out.println("more text"); //more code } catch (IOException e) { //exception handling left as an exercise for the reader }
筆記:
FileWriter
構造函數的第二個參數將告訴它追加到文件中,而不是寫入新文件。 (若是文件不存在,將建立它。) BufferedWriter
用於昂貴的寫入器(例如FileWriter
)。 PrintWriter
可以讓您訪問System.out
可能經常使用的println
語法。 BufferedWriter
和PrintWriter
包裝器不是嚴格必需的。 try { PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("myfile.txt", true))); out.println("the text"); out.close(); } catch (IOException e) { //exception handling left as an exercise for the reader }
若是您須要對舊版Java進行健壯的異常處理,它將變得很是冗長:
FileWriter fw = null; BufferedWriter bw = null; PrintWriter out = null; try { fw = new FileWriter("myfile.txt", true); bw = new BufferedWriter(fw); out = new PrintWriter(bw); out.println("the text"); out.close(); } catch (IOException e) { //exception handling left as an exercise for the reader } finally { try { if(out != null) out.close(); } catch (IOException e) { //exception handling left as an exercise for the reader } try { if(bw != null) bw.close(); } catch (IOException e) { //exception handling left as an exercise for the reader } try { if(fw != null) fw.close(); } catch (IOException e) { //exception handling left as an exercise for the reader } }
您可使用標誌設置爲true
fileWriter
進行追加。
try { String filename= "MyFile.txt"; FileWriter fw = new FileWriter(filename,true); //the true will append the new data fw.write("add a line\n");//appends the string to the file fw.close(); } catch(IOException ioe) { System.err.println("IOException: " + ioe.getMessage()); }