如何将文本附加到Java中的现有文件中

如何将文本附加到Java中的现有文件中

我需要将文本重复附加到Java中的现有文件中。我怎么做?



桃花长相依
浏览 797回答 4
4回答

慕森王

你这样做是为了记录目的吗?如果是这样,那么有几个库。最受欢迎的两个是Log4j和Logback。Java 7+如果您只需要执行此操作,则Files类可以轻松实现: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),建议使用a 。使用a PrintWriter可以访问println您可能习惯的语法System.out。但BufferedWriter和PrintWriter包装是不是绝对必要的。旧Javatry {     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     }}

慕勒3428872

不应该使用try / catch块的所有答案都包含finally块中的.close()块吗?标记答案的示例: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块,因为它是自动处理的,并且也不那么详细:try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("writePath", true)))) {     out.println("the text");} catch (IOException e) {     System.err.println(e);}

qq_遁去的一_1

为了略微扩展Kip的答案,这里有一个简单的Java 7+方法,可以将新行附加到文件中,如果它尚不存在则创建它:try {     final Path path = Paths.get("path/to/filename.txt");     Files.write(path, Arrays.asList("New line to append"), StandardCharsets.UTF_8,         Files.exists(path) ? StandardOpenOption.APPEND : StandardOpenOption.CREATE);} catch (final IOException ioe) {     // Add your own exception handling...}注意:上面使用了Files.write将文本行写入文件的重载(即类似于println命令)。要将文本写到最后(即类似于print命令),Files.write可以使用替代重载,传入字节数组(例如"mytext".getBytes(StandardCharsets.UTF_8))。
打开App,查看更多内容
随时随地看视频慕课网APP