我须要将文本重复添加到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()); }