Hi, I am using an applet which displays a textarea. The value entered in the textarea should be inserted in a text file(file name is passed as a parameter to the applet). If the file already exists, the text file should be read and value should be displayed in the textarea. If not, a new text file should be created, inserting the entered textarea value. I am receiving error message in reading and writing the file. Can you please tell me what is wrong in it. thanks. Here is my code:
public class textarea1 extends Applet { TextArea t1=new TextArea("",4,30); Button b1=new Button("Submit"); String filename; String fin; int ch; String debug;
public void init() { setSize(300,200); add(t1); add(b1);
fin=getParameter("appl_id");
filename="c:\\"+ fin + ".txt"; File tfile=new File(filename); if (tfile.exists()) { debug="file exists"; StringBuffer sb = new StringBuffer(""); FileOutputStream a=new FileOutputStream("tfile"); while((ch = a.read()) != -1) sb.append((char) ch); String s = sb.toString(); t1.setText(s.toString()); a.close(); } else { debug="file not exists"; //File file = new File("fin"); byte[] buf=new byte[1024]; FileInputStream in=new FileInputStream("tfile"); DataInputStream b = new DataInputStream(in);
String s = t1.toString(); for(int i=0;i<s.length();++i) b.write(buf,0,s.charAt(i)); //b.write(buf,0,t1); b.close(); }
} public void actionPerformed(ActionEvent e) {//respond to user entering name repaint(); } public void paint(Graphics g) {//put message on the screen g.drawString(debug, 1, 220); g.drawString(filename, 1, 170); }
In case you are unaware, for an applet to be able to write to file it must be "signed" which requires you to be a member of an organisation whose membership costs $400 per year.
This is not true. You can sign your applet, with your own key. It just won't be certified/validated by someone else. Users will have to explicitly decide at run-time whether they trust you.