Results 1 to 4 of 4
  1. #1
    coolFrenzi is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default [SOLVED] Read/write files using applets

    I have written an application in java which reads files as input and writes to a file as output.
    All the input/output files are at server side. The user just needs to select a file from menu. The result is displayed in a text area.

    I want to display this application on my webpage. Converting the application to applet is not working since applets can't read from and and write to a file.

    Any ideas about how this can be done? (I really don't have any idea of JSP or servlets.)

    Thanks in advance.

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    What have you done so far?show us your code

  3. #3
    coolFrenzi is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default the sample applet with error when read/write a file

    Here is a sample code with the same error (java.security.accessControlException: access denied) when running as an applet.
    The code reads an input file, writes the content to an output file, and then reads the output file and displays the content in an Area box in the applet.

    /*<applet code="text.class" width=500 height=500> </applet> */

    Please let me know how I can implement this. I have gone through a few web pages but I haven't been able to solve it.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JApplet;
    import java.io.*;
    import java.io.IOException;




    public class text extends JApplet
    {

    JLabel TextLabel1;
    JComboBox Combo1;
    TextArea Area1;
    JScrollPane Scroller1;

    BufferedReader in;
    BufferedWriter out;
    String read, iFile1;

    public void init()
    {

    resize(800, 600);

    TextLabel1 = new JLabel("Select an Input file");
    Area1=new TextArea(10,50);
    Scroller1=new JScrollPane(Area1);

    Combo1=new JComboBox();
    Combo1.addItem("red");
    Combo1.addItem("green");
    Combo1.addActionListener(new
    ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    String s = (String)Combo1.getSelectedItem();
    perform(s);
    }
    });


    setLayout(new BorderLayout());
    JPanel pane1 = new JPanel();
    pane1.add(TextLabel1);
    pane1.add(Combo1);
    pane1.add(Area1);
    add(pane1, BorderLayout.NORTH);


    setVisible(true);

    }//end of init


    void perform(String st)
    {

    iFile1=st;

    try
    {
    in = new BufferedReader(new FileReader(iFile1));
    out = new BufferedWriter(new FileWriter("out.txt"));
    while(in.ready())
    {
    read = in.readLine();
    out.write(read);
    out.newLine();//write a new line to the file
    }
    in.close();
    out.close();//flushes and closes the stream

    in = new BufferedReader(new FileReader("out.txt"));
    while(in.ready())
    {
    read = in.readLine();
    Area1.append(read + "\n");
    }
    in.close();
    }

    catch(IOException e)
    {
    System.out.println("There was a problem:" + e);
    }

    }

    }

  4. #4
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by coolFrenzi View Post
    Converting the application to applet is not working since applets can't read from and and write to a file.
    That is a feature of applets. The solution is to not use applets.

    If you are doing browser stuff, have the browser read/write the files.

    You probably need to learn about servlets, which are server side, not client-side the way applets are.

    Programming in the browser should be done in javascript, not in java

Similar Threads

  1. Read and Write file
    By mrdestroy in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 01:11 PM
  2. how do you write applets in Eclipse
    By shashgo in forum Eclipse
    Replies: 0
    Last Post: 09-07-2008, 12:48 AM
  3. File read/write problems
    By p900128 in forum New To Java
    Replies: 4
    Last Post: 06-27-2008, 01:15 AM
  4. Read/Find Substring/Write to new file
    By hiklior in forum New To Java
    Replies: 6
    Last Post: 04-23-2008, 03:47 AM
  5. files and applets, please help
    By willemjav in forum Java Applets
    Replies: 0
    Last Post: 02-07-2008, 11:27 PM

Posting Permissions

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