Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
How to write Into a File in PHP ?
Next article icon

How to Write to a File Using Applet?

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn how to write a file using Applet and grant access to write files using Applet.

Approach

The applet output window contains three fields. Those are the file name field, Text area field, and save button field.

  • File name field: It asks the user to enter the file name to write content in the file.
  • Text area: It is an input field where the content of the file can be entered.
  • Save button: It is an event listener. On clicking the save button, it will call the function that contains the logic to write the file.

Important Note:

  • Latest java version doesn't support applet. so, it needs older version of java which supports applet to be installed.
  • Latest browser will not permit applet application to write files. So, we need to add .policy file with permission granting instructions.

Steps to Run the Program

There are certain Steps to execute the program as mentioned below:

Step 1: Create a policy file with the file name "FileWriterApplet.policy" and add the below code to grant access for writing the file.

FileWriterApplet.policy

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

Step 2: Create an HTML file and replace it with the below code.

FileWriterApplet.html

HTML
<HTML> <HEAD>     <TITLE>File Writer Applet</TITLE>     <applet code="FileWriterApplet.class" width="400" height="300">     </applet> </HEAD> </HTML> 

Step 3: Create FileWriterApplet.java with below code which contains main logic to write the file.

Program for writing to a file using Applet.

Below is the implementaion of the above method:

Java
import java.applet.*; import java.awt.*; import java.awt.event.*; import java.io.*;  public class FileWriterApplet extends Applet {     private TextField fileNameField;     private TextArea textArea;     private Button saveButton;      public void init() {         setLayout(new BorderLayout());          fileNameField = new TextField(20);         textArea = new TextArea(10, 40);         saveButton = new Button("Save");          Panel inputPanel = new Panel();         inputPanel.add(new Label("File Name:"));         inputPanel.add(fileNameField);          add(inputPanel, BorderLayout.NORTH);         add(textArea, BorderLayout.CENTER);         add(saveButton, BorderLayout.SOUTH);          saveButton.addActionListener(new ActionListener() {             public void actionPerformed(ActionEvent e) {                 saveToFile();             }         });     }      private void saveToFile() {         String fileName = fileNameField.getText();         String content = textArea.getText();          try {             FileWriter writer = new FileWriter(fileName);             writer.write(content);             writer.close();             System.out.println("File saved successfully!");         } catch (IOException e) {             System.out.println("Error: " + e.getMessage());         }     } } 

Executing the Program

run-commands
run commands to start applet

Output

Step 1: Enter the file name in the file name field.
Step 2: Write content in the text field.
Step 3. Click on save button to write the file.

Applet Screen

After exectuing the commands in the command line this applet screen will occur.

preview
FIle Writer Applet output

Final Result

The below image shows the file written by the Applet File Writer.

Output-file
Written file

Next Article
How to write Into a File in PHP ?
author
manikandansanmugam
Improve
Article Tags :
  • Java
  • Geeks Premier League
  • java-applet
  • Geeks Premier League 2023
Practice Tags :
  • Java

Similar Reads

  • How to Read a File using Applet?
    In this article, we will learn how to read the content of the file using Java and display those contents using Applet. Approach UsedThe user should type the name of the file in the input field that asks for the filename in the output applet window. If the file is already present, it will display the
    2 min read
  • How to create and write to a txt file using VBA
    This VBA Program reads an Excel Range (Sales Data) and writes to a Text file (Sales.txt). This helps in faster copying of data to a text file as VBA coding enables automated copying. Let us learn how to do it in a few easy steps: Excel VBA Write Text FileIn the realm of Excel VBA, the power to inter
    5 min read
  • How to Display Image using Applet?
    In this article, we will be using the Applet to display the image in proper layout. Here, we will display the image along with the text in the Applet Viewer. Approach for Displaying Images Using AppletWe need to import the essential packages like Applet and AWT for customization.When the packages ge
    4 min read
  • How to Play Audio File (.WAV) Using Java Applet?
    In this article, we will learn how to read and play audio (.wav) files using Java Applet. Approach for Playing Audio File(.wav) using AppletMusicPlayerApplet class inherits the property of Applet in Java. The interface consists of the input text field to enter a filename with an extension of the aud
    3 min read
  • How to write Into a File in PHP ?
    In this article, we are going to discuss how to write into a text file using the PHP built-in fwrite() function. The fwrite() function is used to write into the given file. It stops at the end of the file or when it reaches the specified length passed as a parameter, whichever comes first. The file
    2 min read
  • How to Open a Link in a New Window Using Applet?
    A Java applet is a little application created in the Java programming language and run on a web browser while embedded in an HTML page. In essence, it's a method for introducing Java's "write once, run anywhere" feature to the world of web browsers. Components and OrganizationApplets are subclasses
    9 min read
  • How to Draw a Car using Java Applet?
    An Applet is a Java program that runs in a web browser. An Applet is a Java class that extends the java.applet.Applet class, which means that to create any Applet, you need to import this class and extend it in your Applet class. The applet does not have a main() method. Applets are designed to embe
    2 min read
  • How to Create a Banner Using Applet?
    In this article, we shall be building a Java Applet that shows a scrolling banner with a message that moves continually from right to left. In this article, you will learn the fundamentals of Java Applet Basics by creating a banner using Applet. Note: java.applet package package has been deprecated
    6 min read
  • How to Play Sound using Applet?
    In this article, we will be using the Applet window to play the sound or music. Here, we will have buttons like Play, Pause, and Restart. Using these buttons we will manage the sound in the Applet window. Approach for Playing Sound Using AppletWe need to import the essential packages like Applet and
    4 min read
  • Java File Class toString() Method with Examples
    This method returns the pathname string of the File object is returned by the function toString() function. This method is the same as calling the Java IO getPath() method. Package view is as follows: --> java.io Package --> File Class --> toString() Method Syntax: public String toString()
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences