File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes applet reading files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "applet reading files" Watch "applet reading files" New topic
Author

applet reading files

jite eghagha
Ranch Hand

Joined: Oct 06, 2006
Posts: 127
The lines of code below work when i run my applet, but it dosen't work when i embed it in a jsp page? I can neither read nor write.

What is the correct method i should use to read my .txt file.

All my classes and .txt files are in the same jar folder; and they are in the build folder of my WebApplication.

(I only tried to write to see if i had read/ write access to a folder)

try {
k = new Kruskal();
InputStream in = k.getClass().getResourceAsStream("hs_profile2dist.txt");
if(in == null) {
error += "null";
}
InputStreamReader isr = new InputStreamReader(in);
buff = new BufferedReader(isr);
}catch(Exception e) {
error += e.getMessage();
JOptionPane.showMessageDialog(null, error);
}

try {

BufferedWriter out = new BufferedWriter(new FileWriter("D:/javaExamples/GraphApplet/src/org/me/hello/outfilename4"));
out.write(error);
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
[ June 21, 2007: Message edited by: jite eghagha ]
Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
If your text file is in your applet .jar, then you only need to use a simple file reader to access it. That's what I have done anyway.

is your code generating any error messages? I do not think you are going to be able to write out the file because the path is to the local file system. The applet doesn't know that it is on the same system, so the built in security features are not going to allow it. One way to write a file is to pass the file to a servlet and have the servlet write the file. If you truly want to write a file to the system that is running the applet, you will need to have the applet signed.

Admittedly my experience is limited, so take my input with a grain of salt.
jite eghagha
Ranch Hand

Joined: Oct 06, 2006
Posts: 127
buff = new BufferedReader(new FileReader("hs_profile2dist.txt"));
Gives me a java.io.FileNotFoundException

But i know the file is there cause it works fine when i use the full address:
buff = new BufferedReader(new FileReader("D:/javaExamples/GraphApplet/src/org/me/hello/hs_profile2dist.txt"));

the full address is no good to me when my classes go into a jar file.
[ June 21, 2007: Message edited by: jite eghagha ]
Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
I probably should have asked how you are running your applet; in an ide, from the command line, or html?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41839
    
  63
In a web app you need to use absolute paths, because it's not defined what the default directory might be - it differs between servlet containers. The ServletContext.getRealPath method helps translate between web app paths and absolute paths.

Also, if you use the getResourceAsStream mechanism, use absolute paths like "/hs_profile2dist.txt", not "hs_profile2dist.txt".


Ping & DNS - my free Android networking tools app
jite eghagha
Ranch Hand

Joined: Oct 06, 2006
Posts: 127
I run the applet through my IDE, NetBeans.

About the absolute url;
this url points to my .jsp page
http://localhost:8081/DataClusteringWebApp/index.jsp
this url points to where my .jar file is located
http://localhost:8081/DataClusteringWebApp/build/web/(Jarfile.jar)
and in the .jar file
MyApplet.class, ProcessData.class, data.txt

should my url look like this:
InputStream in = ClusterApplet.class.getResourceAsStream("/hs_profile2dist.txt");

cause that doesn't work.

[ June 21, 2007: Message edited by: jite eghagha ]
[ June 21, 2007: Message edited by: jite eghagha ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: applet reading files