The Artima Developer Community
Sponsored Link

Java Answers Forum
read and write a text file using a textarea field in an applet

4 replies on 1 page. Most recent reply: Jul 31, 2003 6:25 AM by David

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
mahesh

Posts: 1
Nickname: swaya1
Registered: Mar, 2003

read and write a text file using a textarea field in an applet Posted: Mar 19, 2003 8:54 AM
Reply to this message Reply
Advertisement
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:

//File - textarea.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import javax.swing.*;
import java.util.*;


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);
}

}


Ryan

Posts: 7
Nickname: rype69
Registered: Jan, 2003

Re: read and write a text file using a textarea field in an applet Posted: Mar 19, 2003 11:25 AM
Reply to this message Reply
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.

See http://www.suitable.com/Doc_CodeSigning.shtml

Paul

Posts: 1
Nickname: pcyoung
Registered: Jul, 2003

Re: read and write a text file using a textarea field in an applet Posted: Jul 30, 2003 7:29 AM
Reply to this message Reply
If you resolve your filewriting permisions you may also want to fix the following two lines of code:

...
FileOutputStream a=new FileOutputStream("tfile");
...
FileInputStream in=new FileInputStream("tfile");
...

and get rid of the " marks:
FileOutputStream a=new FileOutputStream(tfile);
FileInputStream in=new FileInputStream(tfile);

Greg Lehane

Posts: 33
Nickname: glehane
Registered: Jun, 2003

Re: read and write a text file using a textarea field in an applet Posted: Jul 30, 2003 9:18 AM
Reply to this message Reply
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.

Sign it!

=- Greg

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: read and write a text file using a textarea field in an applet Posted: Jul 31, 2003 6:25 AM
Reply to this message Reply
True.

:-D

Flat View: This topic has 4 replies on 1 page
Topic: Programming Exercises Solution/Answers Previous Topic   Next Topic Topic: menubar can it float?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use