Open txt or csv file and read in Java

S

simon_s_li

Hi,

Does anyone have any coding example on how to select a text or csv
file (file open diagloue box) from my local drive and then read it in
the backend using some kind of inputsteam?

Thanks

Simon
 
B

Bart Van der Donck

Does anyone have any coding example on how to select a text or csv
file (file open diagloue box) from my local drive and then read it in
the backend using some kind of inputsteam?

It should be possible in Java (which your subject indicates) by
embedding an applet into the HTML code.

In javascript it's not possible without making a trip to the web
server.
 
G

gingerpig

What I do with javascript is to have an object tag on the page I want
to load .txt data onto, such as:

<Xdiv>
<Xobject id="objname" width="800" height="300" type="text/html"
data="file:///C:/proof/zinner.html" border="1" >
<X/object>
<X/div>

This loads a second html file [zinner.html] onto the page.

In zimmer.html I have something like the following:

<XSCRIPT id="zlemon" language=javascript type=text/javascript>
<X/SCRIPT>

This script has an id (zlemon) and acts as a target. Also on the page
is:

<Xform name=formGetFile >
<Xinput type=file name="fgf">
<Xinput type=button onClick="getScript()" value="Get Data">
<X/form>

This will allow you to select a file to open, and then trigger the
'get file' operation.

<Xscript>
function getScript() {
zfilaN = document.formGetFile.fgf.value;

k=zfilaN.lastIndexOf('\\');
zfilaN=zfilaN.substring(k+1);

document.getElementById("zlemon").src = zfilaN;
var tid = setTimeout("alert(zfila); window.location.reload();",3000);
};
<X/script>

Basically it strips the drive and directory info to leave the filename
of the file you want to open. Then it sets the src to the script
id='zlemon'... and then it waits for a few seconds just to make sure
that the browser has enough time to access the file you have chosen (i
have used an alert to display the value I wanted retrieved from the
txt file) and then it does a reload to stop the timer and reset
everything.

I have added 'X' to the tag names in case it tries to parse it as html
in the email.

It's crude, but it works in both IE and Firefox. Instead of the alert
you could use something like parent.x = zfila to push the variable
into the parent html file, where 'x' is some variable you have
previously defined in the parent file.

Trevor
 
B

Bart Van der Donck

gingerpig said:
What I do with javascript is to have an object tag on the page I want
to load .txt data onto, such as:

<Xdiv>
<Xobject id="objname" width="800" height="300" type="text/html"
data="file:///C:/proof/zinner.html" border="1" >
<X/object>
<X/div>

This loads a second html file [zinner.html] onto the page.

In zimmer.html I have something like the following:

<XSCRIPT id="zlemon" language=javascript type=text/javascript>
<X/SCRIPT>

This script has an id (zlemon) and acts as a target. Also on the page
is:

<Xform name=formGetFile >
<Xinput type=file name="fgf">
<Xinput type=button onClick="getScript()" value="Get Data">
<X/form>

This will allow you to select a file to open, and then trigger the
'get file' operation.

<Xscript>
function getScript() {
zfilaN = document.formGetFile.fgf.value;

k=zfilaN.lastIndexOf('\\');
zfilaN=zfilaN.substring(k+1);

document.getElementById("zlemon").src = zfilaN;
var tid = setTimeout("alert(zfila); window.location.reload();",3000);};

<X/script>

Basically it strips the drive and directory info to leave the filename
of the file you want to open. Then it sets the src to the script
id='zlemon'... and then it waits for a few seconds just to make sure
that the browser has enough time to access the file you have chosen (i
have used an alert to display the value I wanted retrieved from the
txt file) and then it does a reload to stop the timer and reset
everything.

I have added 'X' to the tag names in case it tries to parse it as html
in the email.

It's crude, but it works in both IE and Firefox. Instead of the alert
you could use something like parent.x = zfila to push the variable
into the parent html file, where 'x' is some variable you have
previously defined in the parent file.

It is very unlikely that this will work in any browser. I tested your
suggestion; the sandbox model seems to work fine with me here
(securing access to local files as it should).

Do you have a demo URL to show your program ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top