Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

saving text files - compiler is returning an error

Status
Not open for further replies.

Aarem

Programmer
Oct 11, 2004
69
US
Hi. I have an applet - save.class. This code should work (i think) but the java compiler is giving me an error. What is wrong with my coding?

Code:
import java.awt.*;
import java.applet.*;
import java.io.*;

public class save extends Applet
{





        BufferedWriter out = new BufferedWriter(new FileWriter("hi.txt"));
        out.write("this should save in hi.txt");
        out.close();


}
 
If you look at the java.applet.Applet, you will see that there are methods there that you are expected to override. That's how you write an Applet.

Maybe you should be doing something like this
Code:
import java.awt.*;
import java.applet.*;
import java.io.*;

public class save extends Applet
{
  public void start(){
    try {
      BufferedWriter out = new BufferedWriter(new FileWriter("hi.txt"));
      out.write("this should save in hi.txt");
      out.close();
    } catch (Exception ex) {
      //handle the problem - log it perhaps!?
    }
  }

}

It's always a good idea to post the errors you get when asking questions on this forum. It helps us to help you faster.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Bear in mind that default applet permissions for that code (ie accessing local file system) will NOT allow the operation, and you must sign the applet to allow it.

--------------------------------------------------
Free Database Connection Pooling Software
 
Thanks for that reminder sedj. It *had* slipped my mind in my effort to point out the correct Java syntax to the OP [blush].

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Yes Stefan, I think you'd use JApplet if you were after a Swing style applet. We'll never know unless Aarem returns [smile].

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
I'm actually not doing this in an applet -- I'm making an application. What are JApplets and Swing-style applets?
 
Your original code was subclassing the Applet class which is how you write AWT Applets. Subclassing JApplet gets you a Swing Applet.

If you're writing a Swing application you probably want to subclass JFrame instead.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top