关于RandomAccessFile对象的问题!!马上给分!

Justin_qj 2003-10-17 02:52:58
RandomAccessFile logFile = new RandomAccessFile("C:\\UInfo.log","rw");
logFile.seek(logFile.length());
logFile.writeBytes("User : "+TxtName.getText()+" at "+LabTime.getText()+" Connection!");

(1)为什么中文写进去是乱码,怎么才能写中文进去!
(2)在文本中的内容怎么是接着写的??我如何写完第一条后换行写第二条呢??


请高手赐教!
...全文
61 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
laughsmile 2004-03-15
  • 打赏
  • 举报
回复
对不起,最后一行应该为logFile.writeBytes(str);
laughsmile 2004-03-15
  • 打赏
  • 举报
回复
String str = "User : "+TxtName.getText()+" at "+LabTime.getText()+" Connection!/";
str = str +"/r/n";
str =new String(str.getBytes("UFT-8"),"UFT-8");
RandomAccessFile logFile = new RandomAccessFile("C:\\UInfo.log","rw");
logFile.seek(logFile.length());
logFile.writeBytes();
Justin_qj 2003-10-17
  • 打赏
  • 举报
回复
logFile.writeBytes("\nUser : "+TxtName.getText()+" at "+LabTime.getText()+" Connection!");

这样不行啊!!请你说详细一点行吗??
LoveRose 2003-10-17
  • 打赏
  • 举报
回复
加换行符就可以了 "\n"
Justin_qj 2003-10-17
  • 打赏
  • 举报
回复
可怎么让指针移到下一行呢??我不想都写在一行上!每写一句换一行!

谢谢!
LoveRose 2003-10-17
  • 打赏
  • 举报
回复
(1):试试用下面这个函数
public final void writeUTF(String str) throws IOException
Writes a string to the file using UTF-8 encoding in a machine-independent manner.
First, two bytes are written to the file, starting at the current file pointer, as if by the writeShort method giving the number of bytes to follow. This value is the number of bytes actually written out, not the length of the string. Following the length, each character of the string is output, in sequence, using the UTF-8 encoding for each character.

(2):每一次写入文件指针都会往后移,所以想继续写入,执行write函数就可以的。

62,615

社区成员

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

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