Answers:
您是否出于记录目的这样做?如果是这样,则有几个库。最受欢迎的两个是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
}
注意: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
可访问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
}
如果您需要对旧版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
}
}
new BufferedWriter(...)
抛出一个异常;会FileWriter
被关闭吗?我猜它不会关闭,因为close()
方法(在正常情况下)将在out
对象上被调用,在这种情况下,该close()
方法不会被初始化-因此实际上该方法将不会被调用->文件将被打开,但是将不会关闭。所以恕我直言,try
声明应该看起来像这样 try(FileWriter fw = new FileWriter("myFile.txt")){ Print writer = new ....//code goes here }
,他应该flush()
是作家,然后再退出该try
区块!!!
您可以将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());
}
try(FileWriter fw = new FileWriter(filename,true)){ // Whatever }catch(IOException ex){ ex.printStackTrace(); }
这里不是所有关于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);
}
out
超出范围时,它会在被垃圾回收时自动关闭,对吗?在您使用finally
块的示例中,out.close()
如果我没有记错的话,我认为您实际上还需要另一个嵌套的try / catch 。Java 7解决方案非常漂亮!(自Java 6以来,我一直没有从事过Java开发,所以我不熟悉该更改。)
flush
方法吗?
编辑 -从Apache Commons 2.1开始,正确的方法是:
FileUtils.writeStringToFile(file, "String to append", true);
我修改了@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();
}
}
}
为了稍微扩展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)
)。
.CREATE
为您工作。
这有点令人震惊,以防万一发生错误,其中有多少答案使文件句柄保持打开状态。答案https://stackoverflow.com/a/15053443/2498188可以赚钱,但这仅仅是因为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
}
}
从Java 7开始,推荐的方法是使用“尝试资源”并让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.
}
PrintWriter.close()
未throws IOException
在docs中声明。从它的源看,该close()
方法确实不能抛出IOException
,因为它是从基础流中捕获它并设置一个标志的。因此,如果您正在为下一个航天飞机或X射线剂量计量系统编写代码,则应PrintWriter.checkError()
在尝试执行之后使用out.close()
。这确实应该被记录在案。
XX.close()
应该在自己的try / catch中,对吗?例如,out.close()
可能抛出异常,在这种情况下bw.close()
,fw.close()
永远不会被调用,fw
这是关闭过程中最关键的一个。
在Java-7中,也可以这样做:
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
// ---------------------
Path filePath = Paths.get("someFile.txt");
if (!Files.exists(filePath)) {
Files.createFile(filePath);
}
Files.write(filePath, "Text to be added".getBytes(), StandardOpenOption.APPEND);
Java 7+
根据我的愚见,因为我是纯Java的粉丝,所以我建议将上述答案组合在一起。也许我参加聚会迟到了。这是代码:
String sampleText = "test" + System.getProperty("line.separator");
Files.write(Paths.get(filePath), sampleText.getBytes(StandardCharsets.UTF_8),
StandardOpenOption.CREATE, StandardOpenOption.APPEND);
如果该文件不存在,则创建它,如果已经存在,则将sampleText附加 到现有文件中。使用此方法,可以避免将不必要的库添加到类路径中。
可以用一行代码完成。希望这可以帮助 :)
Files.write(Paths.get(fileName), msg.getBytes(), StandardOpenOption.APPEND);
使用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();
这产生了BufferedWriter
使用的文件,它接受StandardOpenOption
参数,和自动冲洗PrintWriter
从所得BufferedWriter
。然后可以调用PrintWriter
的println()
方法写入文件。
StandardOpenOption
此代码中使用的参数:打开文件进行写入,仅追加到文件,如果文件不存在则创建文件。
Paths.get("path here")
可以替换为new File("path here").toPath()
。并Charset.forName("charset name")
可以进行修改以适应所需Charset
。
我只添加一些小细节:
new FileWriter("outfilename", true)
2.nd参数(true)是一个称为appendable(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)都可以用于添加内容
换句话说,您可以将一些内容添加到压缩文件中,或添加一些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);
}
尝试使用bufferFileWriter.append,它对我有用。
FileWriter fileWriter;
try {
fileWriter = new FileWriter(file,true);
BufferedWriter bufferFileWriter = new BufferedWriter(fileWriter);
bufferFileWriter.append(obj.toJSONString());
bufferFileWriter.newLine();
bufferFileWriter.close();
} catch (IOException ex) {
Logger.getLogger(JsonTest.class.getName()).log(Level.SEVERE, null, ex);
}
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();
}
这将满足您的预期。
如果我们使用Java 7及更高版本,并且还知道要添加(附加)到文件中的内容,则可以使用NIO包中的newBufferedWriter方法。
public static void main(String[] args) {
Path FILE_PATH = Paths.get("C:/temp", "temp.txt");
String text = "\n Welcome to Java 8";
//Writing to the file temp.txt
try (BufferedWriter writer = Files.newBufferedWriter(FILE_PATH, StandardCharsets.UTF_8, StandardOpenOption.APPEND)) {
writer.write(text);
} catch (IOException e) {
e.printStackTrace();
}
}
有几点要注意:
StandardCharsets
。try-with-resource
语句,其中的资源在尝试后会自动关闭。尽管OP并没有要求,但以防万一我们想搜索具有某些特定关键字的行,例如,confidential
我们可以使用Java中的流API:
//Reading from the file the first line which contains word "confidential"
try {
Stream<String> lines = Files.lines(FILE_PATH);
Optional<String> containsJava = lines.filter(l->l.contains("confidential")).findFirst();
if(containsJava.isPresent()){
System.out.println(containsJava.get());
}
} catch (IOException e) {
e.printStackTrace();
}
write(String string)
如果期望在每个字符串写入后都换行,newLine()
则应调用
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class Writer {
public static void main(String args[]){
doWrite("output.txt","Content to be appended to file");
}
public static void doWrite(String filePath,String contentToBeAppended){
try(
FileWriter fw = new FileWriter(filePath, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)
)
{
out.println(contentToBeAppended);
}
catch( IOException e ){
// File writing/opening failed at some stage.
}
}
}
在项目中的任何位置创建一个函数,并在需要时直接调用该函数。
你们要记住,你们正在调用的活动线程不是异步调用的,因为正确完成该任务可能需要5到10页。为什么不花更多的时间在您的项目上,而忘记写任何已经写的东西。正确地
//Adding a static modifier would make this accessible anywhere in your app
public Logger getLogger()
{
return java.util.logging.Logger.getLogger("MyLogFileName");
}
//call the method anywhere and append what you want to log
//Logger class will take care of putting timestamps for you
//plus the are ansychronously done so more of the
//processing power will go into your application
//from inside a function body in the same class ...{...
getLogger().log(Level.INFO,"the text you want to append");
...}...
/*********log file resides in server root log files********/
第三行代码实际上是第二行,因为第三行实际上是在追加文本。:P
图书馆
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
码
public void append()
{
try
{
String path = "D:/sample.txt";
File file = new File(path);
FileWriter fileWriter = new FileWriter(file,true);
BufferedWriter bufferFileWriter = new BufferedWriter(fileWriter);
fileWriter.append("Sample text in the file to append");
bufferFileWriter.close();
System.out.println("User Registration Completed");
}catch(Exception ex)
{
System.out.println(ex);
}
}
您也可以尝试以下方法:
JFileChooser c= new JFileChooser();
c.showOpenDialog(c);
File write_file = c.getSelectedFile();
String Content = "Writing into file"; //what u would like to append to the file
try
{
RandomAccessFile raf = new RandomAccessFile(write_file, "rw");
long length = raf.length();
//System.out.println(length);
raf.setLength(length + 1); //+ (integer value) for spacing
raf.seek(raf.length());
raf.writeBytes(Content);
raf.close();
}
catch (Exception e) {
//any exception handling method of ur choice
}
我可能会建议apache commons项目。该项目已经提供了执行所需操作的框架(即,集合的灵活过滤)。
以下方法让您将文本追加到某个文件:
private void appendToFile(String filePath, String text)
{
PrintWriter fileWriter = null;
try
{
fileWriter = new PrintWriter(new BufferedWriter(new FileWriter(
filePath, true)));
fileWriter.println(text);
} catch (IOException ioException)
{
ioException.printStackTrace();
} finally
{
if (fileWriter != null)
{
fileWriter.close();
}
}
}
或者使用FileUtils
:
public static void appendToFile(String filePath, String text) throws IOException
{
File file = new File(filePath);
if(!file.exists())
{
file.createNewFile();
}
String fileContents = FileUtils.readFileToString(file);
if(file.length() != 0)
{
fileContents = fileContents.concat(System.lineSeparator());
}
fileContents = fileContents.concat(text);
FileUtils.writeStringToFile(file, fileContents);
}
它效率不高,但效果很好。换行符已正确处理,如果尚不存在,则会创建一个新文件。
这段代码将满足您的需求:
FileWriter fw=new FileWriter("C:\\file.json",true);
fw.write("ssssss");
fw.close();
如果要在特定行中添加某些文本,则可以先阅读整个文件,然后将文本附加到所需的位置,然后覆盖以下代码中的所有内容:
public static void addDatatoFile(String data1, String data2){
String fullPath = "/home/user/dir/file.csv";
File dir = new File(fullPath);
List<String> l = new LinkedList<String>();
try (BufferedReader br = new BufferedReader(new FileReader(dir))) {
String line;
int count = 0;
while ((line = br.readLine()) != null) {
if(count == 1){
//add data at the end of second line
line += data1;
}else if(count == 2){
//add other data at the end of third line
line += data2;
}
l.add(line);
count++;
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
createFileFromList(l, dir);
}
public static void createFileFromList(List<String> list, File f){
PrintWriter writer;
try {
writer = new PrintWriter(f, "UTF-8");
for (String d : list) {
writer.println(d.toString());
}
writer.close();
} catch (FileNotFoundException | UnsupportedEncodingException e) {
e.printStackTrace();
}
}
我的答案:
JFileChooser chooser= new JFileChooser();
chooser.showOpenDialog(chooser);
File file = chooser.getSelectedFile();
String Content = "What you want to append to file";
try
{
RandomAccessFile random = new RandomAccessFile(file, "rw");
long length = random.length();
random.setLength(length + 1);
random.seek(random.length());
random.writeBytes(Content);
random.close();
}
catch (Exception exception) {
//exception handling
}
/**********************************************************************
* it will write content to a specified file
*
* @param keyString
* @throws IOException
*********************************************************************/
public static void writeToFile(String keyString,String textFilePAth) throws IOException {
// For output to file
File a = new File(textFilePAth);
if (!a.exists()) {
a.createNewFile();
}
FileWriter fw = new FileWriter(a.getAbsoluteFile(), true);
BufferedWriter bw = new BufferedWriter(fw);
bw.append(keyString);
bw.newLine();
bw.close();
}// end of writeToFile()
您可以使用follong代码将内容附加到文件中:
String fileName="/home/shriram/Desktop/Images/"+"test.txt";
FileWriter fw=new FileWriter(fileName,true);
fw.write("here will be you content to insert or append in file");
fw.close();
FileWriter fw1=new FileWriter(fileName,true);
fw1.write("another content will be here to be append in the same file");
fw1.close();
1.7方法:
void appendToFile(String filePath, String content) throws IOException{
Path path = Paths.get(filePath);
try (BufferedWriter writer =
Files.newBufferedWriter(path,
StandardOpenOption.APPEND)) {
writer.newLine();
writer.append(content);
}
/*
//Alternative:
try (BufferedWriter bWriter =
Files.newBufferedWriter(path,
StandardOpenOption.WRITE, StandardOpenOption.APPEND);
PrintWriter pWriter = new PrintWriter(bWriter)
) {
pWriter.println();//to have println() style instead of newLine();
pWriter.append(content);//Also, bWriter.append(content);
}*/
}