关于向TXT文档中持续写入数据的程序,一直没有成功 本帖最后由 suexiukjung 于 2011-09-28 17:51:44 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 DefaultEditorKit kit = new DefaultEditorKit();Document doc = ta.getDocument();kit.write(new FileWriter(..), doc,0,doc.getLength()); 我是用RandomAccessFile:RandomAccessFile raf = new RandomAccessFile(file, "rw");raf.seek(raf.length());raf.write((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+message+"\r\n").getBytes("UTF-8"));raf.close(); 还有,在创建文件前先判断下文件是否存在:file = new File(fileName);if (!file.exists()) { try{ file.createNewFile(); }catch(Exception e){ throw new AppException("创建日志文件错误!文件名:"+fileName); }} 用RandomAccessFile和append()都没有成功,能说的稍微详细点吗?? 不清楚楼主是什么问题,难道就是因为向文件中追加内容不成功(只能显示一行)?参考下,可以追加写入文件:import java.io.*;public class Write2File { public static void main(String[] args) { String filename = "xxx.txt"; String textInTextArea = "Hello TextArea", textInTextField = "Hello TextField"; BufferedWriter bw = null; StringBuilder sb = new StringBuilder(1024); try { bw = new BufferedWriter(new FileWriter(filename, true), 1024); for (int i = 0; i < 5; ++i) { sb.append(textInTextArea).append(", ").append(textInTextField); bw.write(sb.toString(), 0, sb.length()); bw.newLine(); sb.delete(0, sb.length()); } } catch (IOException e) { e.printStackTrace(); } finally { if (bw != null) { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } }}运行后查看xxx.txt文件内容:$ cat xxx.txt Hello TextArea, Hello TextFieldHello TextArea, Hello TextFieldHello TextArea, Hello TextFieldHello TextArea, Hello TextFieldHello TextArea, Hello TextField若真是这个的话,那楼主就要补补JAVA中的IO操作了。 谢谢楼上几位的建议,最后我用了stringbuffer 和append()解决了。在左边我又加了个“写入”按钮和该按钮的事件处理。 纠结一个简单的java小程序 关于java播放音频文件的问题 多线程数据同步与获取问题 我遇到了一个棘手的问题,不知怎么办 StringBuffer??? 【求助】谁知道基于SWT的java报表制作工具 环境变量设置问题 请给我的毕业设计一点指导,跪谢!!! JBuilder 2007 +struts 用java读一个比较大的文本文件(几百k~几M)怎么才能比较快? swing中显示高亮的字符显示在视野中 古老的applet,关于cookie的httponly的解决办法
Document doc = ta.getDocument();
kit.write(new FileWriter(..), doc,0,doc.getLength());
raf.seek(raf.length());
raf.write((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+message+"\r\n").getBytes("UTF-8"));
raf.close();
if (!file.exists()) {
try{
file.createNewFile();
}catch(Exception e){
throw new AppException("创建日志文件错误!文件名:"+fileName);
}
}
参考下,可以追加写入文件:import java.io.*;public class Write2File {
public static void main(String[] args) {
String filename = "xxx.txt";
String textInTextArea = "Hello TextArea",
textInTextField = "Hello TextField";
BufferedWriter bw = null;
StringBuilder sb = new StringBuilder(1024);
try {
bw = new BufferedWriter(new FileWriter(filename, true), 1024);
for (int i = 0; i < 5; ++i) {
sb.append(textInTextArea).append(", ").append(textInTextField);
bw.write(sb.toString(), 0, sb.length());
bw.newLine();
sb.delete(0, sb.length());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}运行后查看xxx.txt文件内容:
$ cat xxx.txt
Hello TextArea, Hello TextField
Hello TextArea, Hello TextField
Hello TextArea, Hello TextField
Hello TextArea, Hello TextField
Hello TextArea, Hello TextField若真是这个的话,那楼主就要补补JAVA中的IO操作了。