The Artima Developer Community
Sponsored Link

Java Answers Forum
Applet: Writing to File

4 replies on 1 page. Most recent reply: Sep 19, 2003 8:42 PM by Greg Lehane

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Kevin

Posts: 27
Nickname: quasi
Registered: Apr, 2003

Applet: Writing to File Posted: Sep 19, 2003 4:33 AM
Reply to this message Reply
Advertisement
Does anyone know the code to write to a file on a system using a applet


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Applet: Writing to File Posted: Sep 19, 2003 5:29 AM
Reply to this message Reply
Imagine u loaded an applet and it wrote data in your whole disk??

Cant do that. Security problem boss.

Or can u do that by a signed applet.Hmm i never tried that but have read that this can be done.

http://forum.java.sun.com/thread.jsp?forum=54&thread=383859

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Applet: Writing to File Posted: Sep 19, 2003 5:44 AM
Reply to this message Reply
/**
 * WriteFileApplet.java
 * DG August 2000
 *
 * A simple Java 2 Applet which allows a user to enter some text into a TextArea
 * and save it to a local file, if security permsisions allow it.
 * You can have it write to the working directory , ".\\* in the policy file.
 */
 
 
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.io.*;
 
public class WriteFileApplet extends JApplet  implements ActionListener {
	private JButton btnWrite;
	private JScrollPane sp;
	private JTextArea taMessage;
	private JTextField tfFileName;
	private JPanel pnl;
 
	private String fileName;
 
	public void init () {
		fileName = "C:\\test.txt";
//		fileName = getParameter("filename");
		pnl = new JPanel();
		pnl .setLayout(new BorderLayout());
		pnl.setBorder(BorderFactory.createEtchedBorder());
 
		tfFileName = new JTextField(fileName);
		pnl.add(tfFileName, BorderLayout.NORTH);
 
		// This is how you create a scrollable TextArea in Java 2.
		taMessage = new JTextArea();
		sp = new JScrollPane(taMessage);
		pnl.add(sp, BorderLayout.CENTER);
 
		btnWrite = new JButton("Save text to disk");
		pnl.add(btnWrite, BorderLayout.SOUTH);
 
		// The applet has a default content pane in Java 2.
		getContentPane().add(pnl);
 
		btnWrite.addActionListener(this);
	}
 
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		if(obj instanceof JButton) {
			if(obj == btnWrite) {
				File f = new File(tfFileName.getText());
				try {
					PrintWriter pw = new PrintWriter(new FileWriter(f));
					String msg = taMessage.getText();
					pw.println(msg);
					pw.close();
				} catch(IOException ioe) {
					System.err.println(ioe.toString());
				}
				catch (SecurityException se) {
					System.err.println(se.toString());
				}
			}
		}
	}
	public static void main(String args[])
	{
		WriteFileApplet test = new WriteFileApplet();
		test.init();
		JFrame frame = new JFrame("Applet");
		frame.setContentPane(test);
		frame.setSize(300,300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
}

Kevin

Posts: 27
Nickname: quasi
Registered: Apr, 2003

Re: Applet: Writing to File Posted: Sep 19, 2003 6:06 PM
Reply to this message Reply
I heard there is way way to do it with cgi scripts does anyone know

Greg Lehane

Posts: 33
Nickname: glehane
Registered: Jun, 2003

Re: Applet: Writing to File Posted: Sep 19, 2003 8:42 PM
Reply to this message Reply
Sign your applet. It's the easiest way.

http://java.sun.com/security/signExample12/

=- Greg

Flat View: This topic has 4 replies on 1 page
Topic: How can I select a particular row in a JDBTable ? Previous Topic   Next Topic Topic: jini: how much harm does the scsl?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use