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