[java] Working with Files

Started by
42 comments, last by alexxzius 16 years, 8 months ago
Ok, I was able to get it to work here is the source code that I took from an example tutorial and modified it to do the writing.

Tutorial I used

I also used Java's jdk1.1 tutorial on siging jars
Java's Tutorial

package gdnetfixes;import java.awt.Color;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import javax.swing.BorderFactory;import javax.swing.JApplet;import javax.swing.JButton;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextField;import javax.swing.JTextPane;import javax.swing.border.EtchedBorder;public class AppletWriteTest extends JApplet implements ActionListener {	private JPanel pane = null;	private JScrollPane scrolling = null;	private JTextPane fileBox = null;	private JTextField tfFilename = null;	private JButton butLoad = null;	private final String LOAD = "load";	public void init() {		try {			jbInit();		} catch (Exception e) {			e.printStackTrace();		}	}	// method which will read data from file, and return it in	// String	public String readFile(String fn) {		String thisLine, ret = "";		try {//my changes			FileInputStream fin = new FileInputStream(getCodeBase()					.toExternalForm().replaceFirst("file:(/|\\\\)", "")					+ fn);//end of my changes			BufferedReader myInput = new BufferedReader(new InputStreamReader(					fin));			while ((thisLine = myInput.readLine()) != null) {				ret += thisLine + "\n";			}// my changes			FileOutputStream fout = new FileOutputStream(fn);			PrintWriter pw = new PrintWriter(fout);			pw.println("Hello World! Again!");			pw.flush();			pw.close();// end of my changes		} catch (Exception e) {			System.err.println((new File("")).getAbsolutePath());			System.err.println(getCodeBase());			e.printStackTrace();			ret = "Cannot load, exception!";		}		return ret;	}	private void jbInit() throws Exception {		pane = new JPanel();		pane.setBounds(new Rectangle(0, 0, 500, 325));		pane.setLayout(null);		pane.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));		pane.setBackground(new Color(221, 194, 219));		fileBox = new JTextPane();		fileBox.setText("");		fileBox.setEditable(false);		scrolling = new JScrollPane(fileBox);		scrolling.setBounds(new Rectangle(16, 65, 295, 225));		tfFilename = new JTextField();		tfFilename.setText("");		tfFilename.setBounds(new Rectangle(16, 23, 206, 29));		butLoad = new JButton();		butLoad.setBounds(new Rectangle(231, 23, 80, 30));		butLoad.setText("Load");		butLoad.setActionCommand(LOAD);		butLoad.addActionListener(this);		pane.add(scrolling);		pane.add(tfFilename);		pane.add(butLoad);		setContentPane(pane);	}	public void actionPerformed(ActionEvent e) {		if (e.getActionCommand().equals(LOAD)) {			fileBox.setText(readFile(tfFilename.getText()));		}	}}


These are the commands I used to sign my jar and to create a certificate file.

keytool -genkey -alias signFiles -keystore compstore -keypass kpi135 -dname "cn=jones" -storepass ab987c

jarsigner -keystore compstore -storepass ab987c -keypass kpi135 -signedjar S2RunningDirectory.jar RunningDirectory.jar signFiles

keytool -export -keystore compstore -storepass ab987c -alias signFiles -file CompanyCer.cer

Here is the test page html code:
<applet code="gdnetfixes.AppletWriteTest"        archive="S2RunningDirectory.jar"	width="600"	height="400"	>Get Java Now!</applet>


The getCodeBase() returns the DIRECTORY OF THE WEB PAGE NOT THE JAR FILE. There is no .jar extension, never will you get a location inside the .jar file. And there are ways to write to files within a jar file using java's jar utilities.

Make sure that you try this example all from the same directory and get it working with your own applet before moving the files around.

BTW, its is not hard to change an applet into a stand-a-lone application since Applet is a subclass of java.awt.Panel which you can add to a Frame and run that way, or change extends Applet to extends JFrame or Frame add a main method and a few lines of code and you will be done. I you want I can show you step by step how to go about changing your app into a stand-a-lone app using JFrame instead of Applet but for now I think you will have enough of a challange figuring out the code I just posted.

Oh and relax, reading and writing to a file from an Applet is not trivial, most coders avoid it all together! Good luck!
Advertisement
Quote:Original post by 5MinuteGaming
The getCodeBase() returns the DIRECTORY OF THE WEB PAGE NOT THE JAR FILE. There is no .jar extension, never will you get a location inside the .jar file. And there are ways to write to files within a jar file using java's jar utilities.


Old news by now. By the way, judging from the passwords, we used the same tutorial for the applet signing part =)

Quote:Original post by lightbringer
Old news by now. By the way, judging from the passwords, we used the same tutorial for the applet signing part =)

Yeah it probably was the same tutorial I posted the link above in case you didn't catch it.

@alexxzius
Let me know if you were able to get writing to a file to work in your applet? It's always good to know how things work out.
yeah its working perfectly ;)
Except that i have to use URI (whatever it is), to do the job (like lightbringer told me). Otherwise, no complaints.

This topic is closed to new replies.

Advertisement