java FileWriter 使用出错了,代码如下,我希望在mytxt.txt中末尾处添加数据。求指教

mao906581468 2013-05-07 08:49:52
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class TestFile {
public void outputDataToFile(int data) {

File file1 = new File("G:/mytxt.txt");
if (!file1.exists()) {
try {
System.out.println("试图创建文件");
file1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("文件已存在,直接输入");
// 文件大于指定值时删除后重新创建
if (file1.length() >= 10000000) {
System.out.println("文件太大,试图重新创建文件");
file1.delete();
try {
file1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileWriter writer;
try {
writer = new FileWriter(file1);
String dat = new String(new Double(data).toString());
System.out.println(dat);
// writer.append(" ");
writer.append(dat);
writer.flush();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {

TestFile t = new TestFile();
while (true) {
try {
Thread.sleep(300);
} catch (InterruptedException e) {

e.printStackTrace();
}
int r = (int) (Math.random() * 1000);
t.outputDataToFile(r);
}
}
}
...全文
274 3 打赏 收藏 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
mao906581468 2013-05-07
  • 打赏
  • 举报
回复
感谢1楼和2楼 。我原来的想的是writer = new FileWriter(file1);再调用writer.append(dat);方法,结果在同一个进程内可以在文件末尾追加数据,但关闭程序重新开始的话,就会把文件中的内容清理掉,原来问题是在构造函数上!!!前面想了各种办法都不行,害得我调用dll来做。。。。。再次感谢
lcf 2013-05-07
  • 打赏
  • 举报
回复
writer = new FileWriter(file1, true);
仔细看FileWriter的API解释。有一种是追加,有一种是覆写。
sxl200852 2013-05-07
  • 打赏
  • 举报
回复
这是我修改后的代码,你看看吧!
 package test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class TestFile {
 public void outputDataToFile(int data) {

  File file1 = new File("G:/mytxt.txt");
  if (!file1.exists()) {
   try {
    System.out.println("试图创建文件");
    file1.createNewFile();
   } catch (IOException e) {
    e.printStackTrace();
   }
  } else {
   System.out.println("文件已存在,直接输入");
   // 文件大于指定值时删除后重新创建
   if (file1.length() >= 10000000) {
    System.out.println("文件太大,试图重新创建文件");
    file1.delete();
    try {
     file1.createNewFile();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
  FileWriter writer;
  try {
	  /**FileWriter
public FileWriter(String fileName,
                  boolean append)
           throws IOException根据给定的文件名以及指示是否附加写入数据的 boolean 值来构造 FileWriter 对象。 

参数:
fileName - 一个字符串,表示与系统有关的文件名。
append - 一个 boolean 值,如果为 true,则将数据写入文件末尾处,而不是写入文件开始处。 
抛出: 
IOException - 如果指定文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开它
*/
   writer = new FileWriter(file1,true);
   String dat = new String(new Double(data).toString());
   System.out.println(dat);
   //writer.append(dat);
   writer.write(dat);
   writer.write(" ");
   writer.flush();
   //writer.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 public static void main(String[] args) {

  TestFile t = new TestFile();
  while (true) {
   try {
    Thread.sleep(300);
   } catch (InterruptedException e) {

    e.printStackTrace();
   }
   int r = (int) (Math.random() * 1000);
   t.outputDataToFile(r);
  }
 }
}

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧