关于applet的问题,各位快高手帮忙呀(急用)(200分)

  • 主题发起人 stariver
  • 开始时间
S

stariver

Unregistered / Unconfirmed
GUEST, unregistred user!
我想编一个applet实现留言板功能,用DataOutputStream把文字写入到一个文本文件中,但
我每次存的记录都会把上次的刷掉,不能在上次的基础上追加记录。(即无法把文件指针调到
文件末尾)而且无法加上留言时间
,该代码如下:
import java.util.*;
import java.io.*;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class Class1 extends Applet
{ TextField Name;
Button SendButton;
DataOutputStream Output;
DataInputStream Input;
public void init()
{Name= new TextField(20);
SendButton=new Button("发送");
add(Name);
add(SendButton);

}
public boolean action(Event e,Object o)
{ if (e.target==SendButton)
{
write1();
}
return true;
}
public void write1()
{ String text;
try{


Input= new DataInputStream(new FileInputStream("content.txt"));
Output=new DataOutputStream(new FileOutputStream("content.txt"));
Output.writeChars(Name.getText());
Name.setText("");

}
catch (IOException e) {
showStatus("Exception:"+e.toString());
}
}

}
这两个问题怎么解决呀??
 
>>Output=new DataOutputStream(new FileOutputStream("content.txt"));
Use:
Output=new DataOutputStream(new FileOutputStream("content.txt", true));
to replace it.
append time use:
Calendar rightNow = Calendar.getInstance();
Output.writebytes(rightNow.toString());
 
可以定义自己的Append类,对文件进行Append操作。
class AppendFileStream extends OutputStream {
RandomAccessFile fd;

public AppendFileStream(String file) throws IOException {
fd = new RandomAccessFile(file,"rw");
fd.seek(fd.length());
}

public void close() throws IOException {
fd.close();
}

public void write(byte[] b) throws IOException {
fd.write(b);
}

public void write(byte[] b,int off,int len) throws IOException {
fd.write(b,off,len);
}

public void write(int b) throws IOException {
fd.write(b);
}
}
调用方式:
try {
PrintStream out = new PrintStream(new AppendFileStream("pad_debug.log"));
out.println("apennd string bottom");
out.close();
}
catch(Exception e) {}
 
多谢,我回去试试
 
ok了,但我估计在网站上无法实现,因为他可能是在临时文件夹里运行的,留言内容也肯定保存在
本地计算机上了.无法放在服务器上吧,那应该怎样办呢 ?
 
放弃用applet实现,如果对方的浏览器不支持applet,或者虽然支持但关闭applet功能.
那你的网站怎么办?
 
顶部