How to read file at server? URGENT

hi,

I got stucked to read a file which is at the server, i used this code :

java.io.FileInputStream fis;

String ipaddr="";

int ch;

try

{

fis=new java.io.FileInputStream("server.ini");

while((ch=fis.read())!=-1)

ipaddr=ipaddr+(char)ch;

ipaddr=ipaddr.trim();

System.out.println(ipaddr);

}catch(Exception e){System.out.println(e);}

I'm getting a FileNotFound exception, where i went wrong.

[493 byte] By [hikiran] at [2007-9-26 1:28:34]
# 1

Hello hikiran.

The server.ini file should reside in your application working directory. If it's not there, then the JVM won't find it. Otherwise you could specify an absolute path to the file, but your application will be less portable. Another choice you have is to define an environment variable which tells your app where the server.ini file resides, with the command

java -Dmyapp.ServerIni=C\SomeFolder\server.ini myapp

Then you retrieve the pathname with:

String path = System.getProperty("myapp.ServerIni");

This way your app does not depend on the filesystem on which it's been installed to, because changing the command line that starts your app will be enough to have it working.

Note: if you have to read a text file consider using the java.io.BufferedReader class which lets you to get rid of the different text file formats problems you find when you move to a unix like platform (newline characters are different).

crusca_lucio at 2007-6-29 1:14:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

I'll give u clear picture of what i wan't:

I've developed a full-fledged chat application i've compiled all the classes,

and I have some text file say "server.txt" in which i have

an entry like "192.9.200.100", so when i start my server

i'm reading the server's ipaddress from the "server.txt" file

and binding the server object. Now my client is an applet

where i'm trying to do the same which is giving the FileNotFound

exception, so i've tried in JSP also, i'm getting the same exception

Why I'm doing all this is, so that i can put on any server

and just update the "server.txt" file, so that there is no

need to compile all the java files again.

I think i'm clear, i'm going the rt way or is there any other way.

Its very urgent, plz give some stuff

hikiran at 2007-6-29 1:14:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Let's check if I understand: you have a chat application which already does what you want, however problems come up when you turn it to an applet or jsp.

If this is the case there could be two different causes for your problem:

1 - Applet case: Applets run on the client, so they try to open files on the client. I guess the file server.txt resides on the server... However, even if you move the file on the client, applets cannot read files on the client if they aren't trusted, blah blah blah...

2 - JSP case: JSP pages have their working directory set bye the JSP engine somewhere, but I don't know exaclty where. There you should put your server.txt.

Hope this helps, if not let me know.

crusca_lucio at 2007-6-29 1:14:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
You haven't said which part gives the problems: the server or the applet.If your applet can't read this file: put "server.txt" in your web directory, so you can get it using the URLConnection class
NLSurfMan at 2007-6-29 1:14:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
yes ofcourse "server.txt" file is the webserver directory and when i use :fis=new java.io.FileInputStream("server.ini");its giving FileNotFound Exception;
hikiran at 2007-6-29 1:14:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Try URL.getConnection() instead of the FileInputStream.
crusca_lucio at 2007-6-29 1:14:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...