Loading a txt file into an applet.
as the topic suggests: I want to load a text file into an applet.
i only got this far:
publicstatic String load(String path)
{
String date ="";
File file =new File(path);
try{
// Load
}
catch (IOException e){
e.printStackTrace();
}
return date;
}
how do i load the file?
is it possible to return the data in a list (more convenient)?
Message was edited by:
Comp-Freak
> as the topic suggests: I want to load a text file
> into an applet.
> i only got this far:
>
> public static String load(String path)
>{
> String date = "";
> (path);
>try {
>// Load
>}
>catch (IOException e) {
>e.printStackTrace();
>}
>
>return date;
>}
>
>
> how do i load the file?
> is it possible to return the data in a list (more
> convenient)?
>
> Message was edited by:
> Comp-Freak
First of all, you are going to have to sign your applet if you want access to the local file system (applets run on the client, not the server).
Second of all ... looking at your code, what is "date?" Is it a String (guessing based on your return value...) here is a simple routine to read a text file, line by line:
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("myfile")));
String input;
while( (input = br.readLine() != null ) {
System.out.println(input);
}
} catch (FileNotFoundException fnfe) { System.err.println("File not found."); }
catch (IOException ioe) { ioe.printStackTrace(); }
Well, you will only be able to load a File if you sign your applet. In any other case, mainly if this is about loading configuration data, you should probably be using getClass().getResourceAsStream() instead.
BufferedReader,InputStreamReader,FileInputStream and FileNotFoundException are unknown to my class :-(, waht do i import?
btw: i now that i have to sign my application but i will have to sign it anyway because it needs contact with a server. and no, it's not a configuration file or the like.
> BufferedReader,InputStreamReader,FileInputStream and
> FileNotFoundException are unknown to my class :-(,
> waht do i import?
>
java.io.*
> btw: i now that i have to sign my application but i
> will have to sign it anyway because it needs contact
> with a server. and no, it's not a configuration file
> or the like.
Not exactly. If your applet connects to a server other than the one hosting it, then yes it needs to be signed. If your applet accesses the local file system (i.e. reading a text file), it needs to be signed. If it accesses the system clipboard, then it needs to be signed... the list goes on and on...
On the other hand, if you're simply building a "small java application" then it is not an applet. An applet refers to those applications that extends either Applet or JApplet and (999 times out of 1000) are embedded into a web browser...
> BufferedReader,InputStreamReader,FileInputStream and
> FileNotFoundException are unknown to my class :-(,
> waht do i import?
java.io*?
> btw: i now that i have to sign my application but i
> will have to sign it anyway because it needs contact
> with a server. and no, it's not a configuration file
> or the like.
If this is the server the applet has been downloaded from, there's no need for signing ...
StreamReader,FileInputStream and FileNotFoundException are still unknown...
(input = br.readLine() != null )
^ type mismatch, cannot convert froma string to a boolean
im making an applet, yup. for testing purposes im going to load the textfiles from my local system. when my applet is complete im going to use the URL thingy to load it from a website. i will see how i will get the applet,server and text hosting done. if im lucky its all going to be on one machine... (probably mine)
Sorry, that one was my mistake:while( (input = br.readLine()) != null) // note the extra ) after readLine())
Thanks! it worked out perfectly. In what kind of variable do you suggest i save the lines in?
> Thanks! it worked out perfectly. In what kind of
> variable do you suggest i save the lines in?
It depends on what you want to do with the information. If you are going to read the whole thing and save the entire text of the file, then I would suggest a byte[] array. (Look up a tutorial on buffered input for that.)
If you are only looking for one particular line, save it as a String.
Alternatively, I suppose you could save the lines in a collection of some sort (List, Map or Table) ...
If you've already got key=value pairs, then why not use java.util.Properties? They have a load/store method built into them that allows you to read to / from a Properties file without having to go through that whole routing. (If you are looking for something like properties, then check out this link: http://java.sun.com/j2se/1.4.2/docs/api/java/util/Properties.html)