Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-18-2011, 11:11 PM   PM User | #1
brighty22
New Coder

 
Join Date: Sep 2010
Location: UK
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
brighty22 is an unknown quantity at this point
writing to file - compiles but no file :S

Hi all,
I'm trying to add an export function to an applet, which dumps the contents of an output text field into a .txt file. It compiles properly, but no file gets written

Here's what I've done so far:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class cdDiscount extends Applet implements ActionListener {
    Panel TopPanel = new Panel(), MiddlePanel = new Panel(), BottomPanel = new Panel();
    TextField iCDs = new TextField("Enter number of CDs", 30);
    Button iCalculate = new Button("Calculate"), iClear = new Button("Clear"), iExport = new Button("Export");
    TextArea iResult = new TextArea(20, 30);
    public void init() {
        setSize(400, 400);
        TopPanel.setLayout (new FlowLayout(FlowLayout.CENTER));
        TopPanel.add(iCDs);
        MiddlePanel.setLayout (new FlowLayout(FlowLayout.CENTER));
        MiddlePanel.add(iCalculate);
        MiddlePanel.add(iClear);
        MiddlePanel.add(iExport);
        BottomPanel.setLayout (new FlowLayout(FlowLayout.CENTER));
        BottomPanel.add(iResult);
        this.setLayout(new BorderLayout());
        add(TopPanel,BorderLayout.NORTH);
        add(MiddlePanel,BorderLayout.CENTER);
        add(BottomPanel,BorderLayout.SOUTH);
        iCalculate.addActionListener(this);
        iExport.addActionListener(this);
        iClear.addActionListener(this);
    }
    public void actionPerformed(ActionEvent f) {
        if (f.getSource()== iCalculate) {
            int iInput=Integer.parseInt(iCDs.getText());
            iResult.append(iInput+" CDs = "+Integer.toString(process(Integer.parseInt(iCDs.getText())))+"% discount\n");
        }
        else if (f.getSource()== iExport) {
            export(iResult.getText());
        }
        else
            iResult.setText("");
    }
    public static int process(int iCDs) {
        if(iCDs<=14) return 0;
        else if(iCDs<50) return 1;
        else if(iCDs<120) return 5;
        else return 10;
    }
    public static void export(String iLog) {
        try {
            FileOutputStream iOStream = new FileOutputStream("export.txt");
            new PrintStream(iOStream).println(iLog);
            iOStream.close();
        } catch (IOException k) {}
    }
}
I've used the export() method with another class and it worked.. so am a bit stumped. I think it might be to do with exceptions.. but am probably wrong..

Any help or suggestions would be brilliant
Thanks
brighty22 is offline   Reply With Quote
Old 03-04-2011, 10:04 PM   PM User | #2
spchinta
New Coder

 
Join Date: Mar 2011
Location: USA
Posts: 23
Thanks: 0
Thanked 1 Time in 1 Post
spchinta is an unknown quantity at this point
Not sure if you found the answer or not..

What exception do you get when trying to export?
What is the command you use to run the applet?

This could be a permission issue to write to file from Applet. You need to create a policy file like this: CDDiscount.policy
grant {
permission java.io.FilePermission "<<ALL FILES>>","write";
};
and run applet using command
appletviewer -J-Djava.security.policy=CDDiscount.policy CDDiscount.html
spchinta is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:11 PM.


Advertisement
Log in to turn off these ads.