我正在尝试编写一个方法,如果一个“log.txt文件”尚未存在,则会生成“log.txt文件”然后写入该文件。我遇到的问题是每次调用该方法时,它都会覆盖现有的日志。如何更改方法,以便不是覆盖数据而只是更新文件?
我的写文件方法:
File log = new File("log.txt")
try{
if(log.exists()==false){
System.out.println("We had to make a new file.");
log.createNewFile();
}
PrintWriter out = new PrintWriter(log);
out.append("******* " + timeStamp.toString() +"******* " + "\n");
out.close();
}catch(IOException e){
System.out.println("COULD NOT LOG!!");
}
答案 0 :(得分:37)
只需将PrintWriter out = new PrintWriter(log);
更改为
PrintWriter out = new PrintWriter(new FileWriter(log, true));
答案 1 :(得分:10)
改为使用FileWriter。
FileWriter(File file, boolean append)
构造函数中的第二个参数告诉FileWriter将任何给定的输入附加到文件而不是覆盖它。
这是您的示例的一些代码:
File log = new File("log.txt")
try{
if(!log.exists()){
System.out.println("We had to make a new file.");
log.createNewFile();
}
FileWriter fileWriter = new FileWriter(log, true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("******* " + timeStamp.toString() +"******* " + "\n");
bufferedWriter.close();
System.out.println("Done");
} catch(IOException e) {
System.out.println("COULD NOT LOG!!");
}
答案 2 :(得分:2)
出于某种原因,其他方法都没有为我工作......所以我尝试了这个并且工作了。希望它有所帮助..
JFileChooser c= new JFileChooser();
c.showOpenDialog(c);
File write_file = c.getSelectedFile();
String Content = "Writing into file\n hi \n hello \n hola";
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) {
System.out.println(e);
}
答案 3 :(得分:2)
JFileChooser c= new JFileChooser();
c.showOpenDialog(c);
File write_file = c.getSelectedFile();
String Content = "put here the data to be wriiten";
try
{
FileWriter fw = new FileWriter(write_file);
BufferedWriter bw = new BufferedWriter(fw);
bw.append(Content);
bw.append("hiiiii");
bw.close();
fw.close();
}
catch(Exception e)
{
System.out.println(e);
`}
答案 4 :(得分:1)
这是一个简单的例子,它是如何工作的,最好的做法是将try \ catch放入其中但是对于基本用途,这应该可以解决问题。为此,您有一个字符串和文件路径,因此适用于FileWriter和BufferedWriter。这将写入“Hello World”(数据变量),然后创建一个新行。每次运行时,它都会将Data变量添加到下一行。
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
String Data = "Hello World";
File file = new File("C:/Users/stuff.txt");
FileWriter fw = new FileWriter(file,true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(Data);
bw.newLine();
bw.close();
答案 5 :(得分:0)
尝试这个
public void writeFile(String arg1,String arg2) {
try {
if (!dir.exists()) {
if (dir.mkdirs()) {
Toast.makeText(getBaseContext(), "Directory created",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getBaseContext(),
"Error writng file " + filename, Toast.LENGTH_LONG)
.show();
}
}
else {
File file = new File(dir, filename);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fileWritter = new FileWriter(file, true);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
bufferWritter.write(arg1 + "\n");
bufferWritter.close();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(),
"Error writng file " + e.toString(), Toast.LENGTH_LONG)
.show();
}
}
答案 6 :(得分:0)
您甚至可以使用FileOutputStream
来获得所需内容。这就是它的完成方式,
File file = new File(Environment.getExternalStorageDirectory(), "abc.txt");
FileOutputStream fOut = new FileOutputStream(file, true);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write("whatever you need to write");
osw.flush();
osw.close();
答案 7 :(得分:0)
BufferedWriter login = new BufferedWriter(new FileWriter("login.txt"));
如果要在一行中创建文件,就是一个例子。
答案 8 :(得分:0)
您可以更改PrintWriter并使用方法getAbsoluteFile(),此函数将返回给定抽象路径名的绝对File对象。
PrintWriter out = new PrintWriter(new FileWriter(log.getAbsoluteFile(), true));