Home Java Example Java Applet Applet Write Files Example
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Applet Write Files Example

Advertisement
In this section, you will learn how to write to a file from an applet.

Applet Write Files Example

     

In this section, you will learn how to write to a file from an applet. This is very simple but the security manager don't provide the permission for writing a file from an applet. For this, you will have to maintain a Java security policy file that will allow the granted permission for writing a file from an applet. Without maintaining the Java security policy file, the security manager naturally installed during startup whenever an applet is started in the Java based web browser. By default the file writing permission is not allowed. All the permissions are allowed or disallowed in the policy file after that you write it or not.

Description of program:

The following program or code helps you in writing the content in a file from an applet. First of all this program creates a class (WriteFile) to extends the Applet class. After that, this program constructs a GUI layout that contains text field, text area and a command button. The text field has the name of file that have to be written and the text area provides a area for writing the user text that have to write in the given file. The common button performs your action ( for writing ). When you will run it (use the appletviewer -J-Djava.security.policy=po.policy WriteFileApplet.html), the GUI appears on the screen that takes the file name and text (content) on the specified locations. Without giving it, a message will appear on the screen in the message box (Please enter file name!). If the file name is entered without any text, even though it will  display a message in the message box (Please enter your text!). You have entered both and click the "WriteToFile" command button, the file name is checked out. Whenever the file isn't exists, it will display a message in the message box (File not found!) and if the given file exists, the given text are written into the file and it will show a message in the message box (Text is written in vk.shtml) otherwise not and shows a message in the message box (Text  isn't written in vk.shtml). You will try it.

Here is the code of program (WriteFile.java):

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;
import java.net.*;

public class WriteFile extends Applet{
  Button write = new Button("WriteToFile");
  Label label1 = new Label("Enter the file name:");
  TextField text = new TextField(20);
  Label label2 = new Label("Write your text:");
  TextArea area = new TextArea(10,20);
  public void init(){
  add(label1);
  label1.setBackground(Color.lightGray);
  add(text);
  add(label2);
  label2.setBackground(Color.lightGray);
  add(area);
  add(write,BorderLayout.CENTER);
  write.addActionListener(new ActionListener (){
  public void actionPerformed(ActionEvent e){
  new WriteText();
  }
  });
  }
  public class WriteText {
  WriteText(){
  try {
  String str = text.getText();
  if(str.equals("")){
  JOptionPane.showMessageDialog(null,"Please enter the file name!");
  text.requestFocus();
  }
  else{
  File f = new File(str);
  if(f.exists()){
  BufferedWriter out = new BufferedWriter(new FileWriter(f,true));
  if(area.getText().equals("")){
  JOptionPane.showMessageDialog(null,"Please enter your text!");
  area.requestFocus();
  }
  else{
  out.write(area.getText());
  if(f.canWrite()){
  JOptionPane.showMessageDialog(null,"Text is written in "+str);
  text.setText("");
  area.setText("");
  text.requestFocus();
  }
  else{
  JOptionPane.showMessageDialog(null,"Text isn't written in "+str);
  }
  out.close();
  }
  }
  else{
  JOptionPane.showMessageDialog(null,"File not found!");
  text.setText("");
  text.requestFocus();
  }
  }
  }
  catch(Exception x){
  x.printStackTrace();
  }
  }
  }
}

Download this code.

For running an applet you have to need an HTML file that uses the applet class to run this in the Java based web browser.

Here is the HTML code (WriteFile.html):

<HTML>
<HEAD>
<TITLE>
Write file example </TITLE>
<applet code="WriteFile.class", width="200",height="300">
</applet>
</HEAD>
</HTML>

This is the policy file that allowed the permissions to the user for writing or reading the file from an applet.

Here is the code of policy file (WriteFile.policy):

grant {
permission java.io.FilePermission "<<ALL FILES>>","write";
};

Output of program:

Compile and run:

C:\vinod\applet>javac WriteFile.java

C:\vinod\applet>appletviewer -J-Djava.security.policy=WriteFile.policy WriteFile.html

appletviewer:
This is the command that allows you for running the applets outside of a web browser.

-Jjavaoption:
The javaoption is a single argument string to the Java interpreter that runs the appletviewer. This argument hasn't spaces, if the argument contains the multiple words must be begin with '-J' prefix that is stripped and useful for adjusting the execution of compiler environment or memory usage.

If file name is blank:

If Text area is blank:

Both are written:

Message Box:

Get the written file: 
vk.shtml

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 14, 2007

Ask Questions?    Discuss: Applet Write Files Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
hk naik
September 12, 2011
1 error in your code

C:\jdk.1.2.2\bin>javac WriteFile.java WriteFile.java:39: Incompatible type for constructor. Can't convert java.io.File to java.lang.String. BufferedWriter out = new BufferedWriter( new FileWriter(f,true)); ^ 1 error
Vicky Deshwali
February 13, 2013
java applet

How run the applet file program?
Erico
November 9, 2011
Write file from web browser

Hello. Thanks for your tutorial, it helped me a lot! I run the applet from the Applet Viewer and worked just fine. I wanted to do a web page that writes a helloworld.txt in disk, so I uploaded the .class, .html and java.policy.applet to the server (XAMPP 1.7.7 Linux) and typed "http://localhost/teste/WriteFile.html" in URL. The applet was initialized, but no file was wrote. Could you please help me? Thank you, Erico
Prakash Prajapati
December 23, 2011
Writing Object Using Applet

Hi i am prakash how i can Writing Object in the file Using Applet. when i click on "ok" button at that time the object is writen in the file. please help me.
Armando
May 30, 2012
cant get it to work

the applet apears on the html but it wont work. dose it have something to do with the policy?
armando
June 8, 2012
dont u need to sign the applet?

i cant get it to run i used the policy file i signed the applet and when it runs in a html doesnt do nothing
rama krishna
April 11, 2014
doubt

where i can write the code of policy file
DMCA.com