Kip*_*Kip 770
你这样做是为了记录目的吗?如果是这样,那么有几个库.最受欢迎的两个是Log4j和Logback.
如果您只需要执行此操作,则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
}
Run Code Online (Sandbox Code Playgroud)
小心: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
}
Run Code Online (Sandbox Code Playgroud)
笔记:
FileWriter构造函数的第二个参数将告诉它附加到文件,而不是写一个新文件.(如果该文件不存在,则会创建该文件.)BufferedWriter对于昂贵的作家(例如FileWriter),建议使用a .PrintWriter可以访问println您可能习惯的语法System.out.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
}
Run Code Online (Sandbox Code Playgroud)
如果您需要针对较旧的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
}
}
Run Code Online (Sandbox Code Playgroud)
nor*_*ole 162
您可以使用fileWriter设置为的标志true进行追加.
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());
}
Run Code Online (Sandbox Code Playgroud)
ete*_*ech 69
不应该使用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();
}
}
Run Code Online (Sandbox Code Playgroud)
此外,从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);
}
Run Code Online (Sandbox Code Playgroud)
rip*_*234 42
编辑 - 从Apache Commons 2.1开始,正确的方法是:
FileUtils.writeStringToFile(file, "String to append", true);
Run Code Online (Sandbox Code Playgroud)
我改编了@Kip的解决方案,包括最终正确关闭文件:
public static void appendToFile(String targetFile, String s) throws IOException {
appendToFile(new File(targetFile), s);
}
public static void appendToFile(File targetFile, String s) throws IOException {
PrintWriter out = null;
try {
out = new PrintWriter(new BufferedWriter(new FileWriter(targetFile, true)));
out.println(s);
} finally {
if (out != null) {
out.close();
}
}
}
Run Code Online (Sandbox Code Playgroud)
Ste*_*ers 28
为了略微扩展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...
}
Run Code Online (Sandbox Code Playgroud)
注意:上面使用了Files.write将文本行写入文件的重载(即类似于println命令).要将文本写到最后(即类似于print命令),Files.write可以使用替代重载,传入字节数组(例如"mytext".getBytes(StandardCharsets.UTF_8)).
Emi*_* L. 21
令人担忧的是,如果出现错误,这些答案中有多少会使文件句柄保持打开状态.答案/sf/answers/1053741041/是钱,但只是因为BufferedWriter()不能扔.如果可能则异常将使FileWriter对象保持打开状态.
执行此操作的更一般方法不关心是否BufferedWriter()可以抛出:
PrintWriter out = null;
BufferedWriter bw = null;
FileWriter fw = null;
try{
fw = new FileWriter("outfilename", true);
bw = new BufferedWriter(fw);
out = new PrintWriter(bw);
out.println("the text");
}
catch( IOException e ){
// File writing/opening failed at some stage.
}
finally{
try{
if( out != null ){
out.close(); // Will close bw and fw too
}
else if( bw != null ){
bw.close(); // Will close fw too
}
else if( fw != null ){
fw.close();
}
else{
// Oh boy did it fail hard! :3
}
}
catch( IOException e ){
// Closing the file writers failed for some obscure reason
}
}
Run Code Online (Sandbox Code Playgroud)
从Java 7开始,推荐的方法是使用"try with resources"并让JVM处理它:
try( FileWriter fw = new FileWriter("outfilename", true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)){
out.println("the text");
}
catch( IOException e ){
// File writing/opening failed at some stage.
}
Run Code Online (Sandbox Code Playgroud)
Tso*_*yan 13
在Java-7中它也可以这样做:
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
Run Code Online (Sandbox Code Playgroud)
// ---------------------
Path filePath = Paths.get("someFile.txt");
if (!Files.exists(filePath)) {
Files.createFile(filePath);
}
Files.write(filePath, "Text to be added".getBytes(), StandardOpenOption.APPEND);
Run Code Online (Sandbox Code Playgroud)
java 7+
在我的拙见中,因为我是普通java的粉丝,我建议它是上述答案的组合.也许我迟到了.这是代码:
String sampleText = "test" + System.getProperty("line.separator");
Files.write(Paths.get(filePath), sampleText.getBytes(StandardCharsets.UTF_8),
StandardOpenOption.CREATE, StandardOpenOption.APPEND);
Run Code Online (Sandbox Code Playgroud)
如果该文件不存在,则创建该文件,如果该文件已存在,则将sampleText附加 到现有文件.使用它,可以避免在类路径中添加不必要的库.
这可以在一行代码中完成.希望这可以帮助 :)
Files.write(Paths.get(fileName), msg.getBytes(), StandardOpenOption.APPEND);
Run Code Online (Sandbox Code Playgroud)
使用java.nio.文件以及java.nio.file.StandardOpenOption
PrintWriter out = null;
BufferedWriter bufWriter;
try{
bufWriter =
Files.newBufferedWriter(
Paths.get("log.txt"),
Charset.forName("UTF8"),
StandardOpenOption.WRITE,
StandardOpenOption.APPEND,
StandardOpenOption.CREATE);
out = new PrintWriter(bufWriter, true);
}catch(IOException e){
//Oh, no! Failed to create PrintWriter
}
//After successful creation of PrintWriter
out.println("Text to be appended");
//After done writing, remember to close!
out.close();
Run Code Online (Sandbox Code Playgroud)
这将创建一个BufferedWriter使用Files,它接受StandardOpenOption参数,并PrintWriter从结果中自动刷新BufferedWriter.PrintWriter的println()方法,然后可以调用写入文件.
StandardOpenOption此代码中使用的参数:打开要写入的文件,仅附加到文件,如果文件不存在则创建该文件.
Paths.get("path here")可以替换为new File("path here").toPath().并且Charset.forName("charset name")可以进行修改以适应所需的Charset.
我只是添加小细节:
new FileWriter("outfilename", true)
Run Code Online (Sandbox Code Playgroud)
2.nd参数(真)是一个功能(或接口)称为追加(http://docs.oracle.com/javase/7/docs/api/java/lang/Appendable.html).它负责能够将某些内容添加到特定文件/流的末尾.此接口从Java 1.5开始实现.具有此接口的每个对象(即BufferedWriter,CharArrayWriter,CharBuffer,FileWriter,FilterWriter,LogStream,OutputStreamWriter,PipedWriter,PrintStream,PrintWriter,StringBuffer,StringBuilder,StringWriter,Writer)都可用于添加内容
换句话说,您可以向gzip压缩文件或某些http进程添加一些内容
样品,使用番石榴:
File to = new File("C:/test/test.csv");
for (int i = 0; i < 42; i++) {
CharSequence from = "some string" + i + "\n";
Files.append(from, to, Charsets.UTF_8);
}
Run Code Online (Sandbox Code Playgroud)