Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2010
    Location
    UK
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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

  • #2
    New Coder
    Join Date
    Mar 2011
    Location
    USA
    Posts
    23
    Thanks
    0
    Thanked 1 Time in 1 Post
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •