Prasad Mene
October 11th, 1999, 07:36 AM
Hello,
I created a simple applet in swing and it run fine in my swing
enabled browser.But when i tried something on reading and writing
to a local file through an applet, it didn't work.I have also
created a ploicy file with policytool.The applet is working well in the appletviewer.But when i tried to run it through my browser ,it didn't work.Where should i place my policy file?I am using<pre> <object> </object><pre> tag to load the applet in IE5.There is a text field and button on my applet,which when clicked on entering text in textbox will store it in local file and echo the same to me.
It only loads the applet and stop functioning.If anyone can please
help me or redirect me to some site where i will get some help i will be very grateful to you.THE JAVA FILE IS AS BELOW.
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.applet.Applet;
public class app1_fileio extends Applet
implements ActionListener{
public JLabel text,clicked;
public JButton button,clickButton;
public JTextField textField;
public void init(){
setLayout(new BorderLayout(1,1));
setBackground(Color.white);
text = new JLabel("Iam simple program");
clicked = new JLabel("Button Clicked");
button = new JButton("Click me");
//add action listener
button.addActionListener(this);
clickButton = new JButton("Click again");
//add action listener
clickButton.addActionListener(this);
textField = new JTextField(20);
//add label and button to panel
add("North",text);
add("Center",textField);
add("South",button);
} //end of cnst
public void actionPerformed(ActionEvent eve)
{
Object source = eve.getSource();
JLabel label = new JLabel();
if(source == button)
{
try{
//get text from textField
String txt = textField.getText();
byte b[] = txt.getBytes();
//create new file object
String opfilenm = new String("result.txt");
//create new file object
File opfile = new File(opfilenm);
//attach this file object to opstream
FileOutputStream out = new FileOutputStream(opfile);
out.write(b);
out.close();
//code to read from the file
String ipfilenm = new String("result.txt");
//create new file object
File ipfile = new File(ipfilenm);
//attach this to ipstream
FileInputStream ip = new FileInputStream(ipfile);
byte bb[] = new byte[(int)ipfile.length()];
int i;
i = ip.read(bb);
String dis = new String(bb);
label.setText(dis);
ip.close();
}catch(java.io.IOException e){
System.out.println("Cannot access result.txt");
}
removeAll();
add("North",label);
add("Center",label);
add("South",clickButton);
validate();
repaint();
}
if(source == clickButton)
{
removeAll();
add("North",text);
textField.setText("");
add("Center",textField);
add("South",button);
validate();
repaint();
}
}
public void start(){
System.out.println("Strating applet.....");
}
public void stop(){
System.out.println("Applet Stopped......");
}
public void destroy(){
System.out.println("Applet destroyed....");
}
}//end of class
I created a simple applet in swing and it run fine in my swing
enabled browser.But when i tried something on reading and writing
to a local file through an applet, it didn't work.I have also
created a ploicy file with policytool.The applet is working well in the appletviewer.But when i tried to run it through my browser ,it didn't work.Where should i place my policy file?I am using<pre> <object> </object><pre> tag to load the applet in IE5.There is a text field and button on my applet,which when clicked on entering text in textbox will store it in local file and echo the same to me.
It only loads the applet and stop functioning.If anyone can please
help me or redirect me to some site where i will get some help i will be very grateful to you.THE JAVA FILE IS AS BELOW.
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.applet.Applet;
public class app1_fileio extends Applet
implements ActionListener{
public JLabel text,clicked;
public JButton button,clickButton;
public JTextField textField;
public void init(){
setLayout(new BorderLayout(1,1));
setBackground(Color.white);
text = new JLabel("Iam simple program");
clicked = new JLabel("Button Clicked");
button = new JButton("Click me");
//add action listener
button.addActionListener(this);
clickButton = new JButton("Click again");
//add action listener
clickButton.addActionListener(this);
textField = new JTextField(20);
//add label and button to panel
add("North",text);
add("Center",textField);
add("South",button);
} //end of cnst
public void actionPerformed(ActionEvent eve)
{
Object source = eve.getSource();
JLabel label = new JLabel();
if(source == button)
{
try{
//get text from textField
String txt = textField.getText();
byte b[] = txt.getBytes();
//create new file object
String opfilenm = new String("result.txt");
//create new file object
File opfile = new File(opfilenm);
//attach this file object to opstream
FileOutputStream out = new FileOutputStream(opfile);
out.write(b);
out.close();
//code to read from the file
String ipfilenm = new String("result.txt");
//create new file object
File ipfile = new File(ipfilenm);
//attach this to ipstream
FileInputStream ip = new FileInputStream(ipfile);
byte bb[] = new byte[(int)ipfile.length()];
int i;
i = ip.read(bb);
String dis = new String(bb);
label.setText(dis);
ip.close();
}catch(java.io.IOException e){
System.out.println("Cannot access result.txt");
}
removeAll();
add("North",label);
add("Center",label);
add("South",clickButton);
validate();
repaint();
}
if(source == clickButton)
{
removeAll();
add("North",text);
textField.setText("");
add("Center",textField);
add("South",button);
validate();
repaint();
}
}
public void start(){
System.out.println("Strating applet.....");
}
public void stop(){
System.out.println("Applet Stopped......");
}
public void destroy(){
System.out.println("Applet destroyed....");
}
}//end of class