Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Location of File

If I were to do

BufferedWriter out = new BufferedWriter(new FileWriter(new File("someFile.txt")));

try {
out.write("something");
out.close();
} catch (IOException e) {}

and this was in an applet on a website, where would the file someFile.txt be located?
[301 byte] By [destin] at [2007-11-11 7:38:57]
# 1 Re: Location of File
The web page's "source" or other location on the web server to which the client is given access
nspils at 2007-11-11 22:37:40 >
# 2 Re: Location of File
So if you were to use this to write a high scores list or something, it would work?
destin at 2007-11-11 22:38:41 >
# 3 Re: Location of File
Yes ... it would be shared among all potential users of the applet
nspils at 2007-11-11 22:39:50 >
# 4 Re: Location of File
If an applet tries to do this (opening a file on the client file system) the SecurityManager would have it shot on the spot.
sjalle at 2007-11-11 22:40:50 >
# 5 Re: Location of File
And even if the security settings were set to allow local file access, it still wouldn't do what Destin wants because the file is saved locally. An applet runs on the users PC. So to save it on a web server - you would need to open a port to the server, either HTTP or TCP/IP.
mnuttall at 2007-11-11 22:41:49 >
# 6 Re: Location of File
Thanks guys, but just so you know, I don't really need this, just curious. :D
destin at 2007-11-11 22:42:48 >
# 7 Re: Location of File
Wait, so then how would one go about making a high scores list or something (in an applet on a webpage)?
destin at 2007-11-11 22:43:46 >
# 8 Re: Location of File
Load/display/modify/save a file shared from the source directory of the web page server
nspils at 2007-11-11 22:44:49 >
# 9 Re: Location of File
... And use a servlet (via http or tcp/ip) to send the file contents to the applet or recieve the info from the applet.
mnuttall at 2007-11-11 22:45:51 >
# 10 Re: Location of File
Unless you have your own server it is not easy to find a web host that supports servlets. Maybe I'm out of line here (this being a java forum) but with php you can have this done easily. Here is the php code:

<?php
/**
** Get contents of a file in the http://<hostname>/docs/ directory
** and return the contents to the client
**/
$fileName=$_REQUEST['file_name'];
$fPath=$_SERVER['DOCUMENT_ROOT']."/docs/".$fileName;
$fp=fopen($fPath,"r");
$content=fread($fp,filesize($fPath));
fclose($fp);
echo $content;
?>

here is the applet code:

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

public class GetFileApplet extends JApplet implements ActionListener {
private boolean isStandalone = false;
JPanel jPanel1 = new JPanel();
JLabel jLabel1 = new JLabel();
JTextField filenameTF = new JTextField();
JButton getFileBtn = new JButton();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea repTA = new JTextArea();
JButton clearBtn = new JButton();
GridBagLayout gridBagLayout1 = new GridBagLayout();


public GetFileApplet() {
}

public void init() {
try {
jbInit();
clearBtn.addActionListener(this);
getFileBtn.addActionListener(this);
}
catch(Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==getFileBtn) {
String s = null;
try {
s = getContents().toString();
repTA.append(s);
}
catch (Exception ex) {
repTA.append("\n****************** ERROR ******************");
repTA.append("\nGetting file: "+filenameTF.getText().trim()+" failed: ");
repTA.append("\n"+ex.getMessage());
ex.printStackTrace();
}
} else if (e.getSource()==clearBtn) {
repTA.setText("");
}
}
/**
* Get server side file
*/
public StringBuffer getContents() throws Exception {
StringBuffer buffer;
String line;
int responseCode;
HttpURLConnection connection;
BufferedReader dataInput;

String hostName=getDocumentBase().getHost();
String p=getDocumentBase().getPath();
String fileName=filenameTF.getText().trim();
int ix = p.lastIndexOf("/");
String pth = "http://" + hostName + "/getFile.php?file_name="+fileName;

URL url = new URL(pth);

connection = (HttpURLConnection)url.openConnection();
responseCode = connection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new Exception("HTTP response code: " +
String.valueOf(responseCode));
}
try {
buffer = new StringBuffer();
InputStream input = connection.getInputStream();
dataInput = new BufferedReader(new InputStreamReader(input));

while ( (line = dataInput.readLine()) != null) {
buffer.append(line);
buffer.append('\n');
//System.out.println("line: " + line);
}
input.close();
} catch (Exception ex) {
ex.printStackTrace(System.err);
return null;
}
return buffer;
}

//Component initialization
private void jbInit() throws Exception {
this.setSize(new Dimension(400,300));
jPanel1.setLayout(gridBagLayout1);
jLabel1.setText("File Name");
getFileBtn.setText("Get File");
filenameTF.setText("");
repTA.setMargin(new Insets(2, 2, 2, 2));
repTA.setText("");
clearBtn.setText("Clear");
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jLabel1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(8, 16, 0, 0), 3, 1));
jPanel1.add(filenameTF, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(8, 8, 0, 0), 171, -5));
jPanel1.add(getFileBtn, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(8, 5, 0, 8), 35, -7));
jPanel1.add(jScrollPane1, new GridBagConstraints(0, 1, 3, 1, 1.0, 1.0
,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(14, 16, 0, 14), 360, 193));
jScrollPane1.getViewport().add(repTA, null);
jPanel1.add(clearBtn, new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(6, 16, 7, 137), 43, -7));
}

}

and here is the html.

<html>
<head>
<title>
HTML Test Page
</title>
</head>
<body>
Getting a file from the server<br>
<applet
codebase = "."
code = "GetFileApplet.class"
name = "getFile"
width = "400"
height = "300"
hspace = "0"
vspace = "0"
align = "middle"
>
</applet>
</body>
</html>
sjalle at 2007-11-11 22:46:52 >
# 11 Re: Location of File
I would say that it is not out of line. I was going to suggest something like that ,if they just wanted to display scores, but I figured there was enough confusion. :) If they are just going to display scores, I have no idea why they are using an applet. Maybe for some kewl effects.
mnuttall at 2007-11-11 22:47:56 >
# 12 Re: Location of File
Hmm... where are cookies saved?
destin at 2007-11-11 22:48:54 >
# 13 Re: Location of File
On my win2000 it seems to be here:

C:\Documents and Settings\Administrator\cookies
sjalle at 2007-11-11 22:49:55 >