Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

reading files  RSS feed

 
pradeep raju
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
i want to read a file which is in current directory that also has other java programs.How can i get the file object for that file with out giving the whole directory i.e i dont want to give
File f1=new File("c:\mypack\first\eample.txt");

i might move this application any other systems.i cant chnge the path each time i move the application to other syustem or other directory.
please help. bye bye
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

If it's in the current directory, i.e. the one the application is started in, why specify a directory at all? You could just use "new File("eample.txt")".
[ February 07, 2006: Message edited by: Ulf Dittmer ]
 
pradeep raju
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i should have been more clear.I am building application using struts framework.Iam using eclipse tool.
my code is
File docFile = new File("stocks.xml");
System.out.println("abs path "+docFile.getAbsolutePath());

and guess the output

abs path D:\eclipse\stocks.xml

my package hirarchy is c:/raju/stocks/stocks.xml
so iam forced to give the entire path.
File docFile = new File("C:\\raju\\stocks\\stocks.xml");
i will face problem if stocks folder is moved to other directory in my system or in any other unknown system.
Hope u understood my prob..plz reply soon.thank you
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this is a web application. You could make the path an init parameter of a servlet in that web app, so that whenever the directory moves, you change your web.xml, cycle the web app, and are good to go. Alternatively, keep it in a properties file that gets read by the web app at startup time.
 
pradeep raju
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
thank you very much for your suggestion..I want to do that with the properties file..but i dont know how to do that.will u tell me
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The steps to take would be:
1) create a properties file containing something like "xml.file=C:\\\\path\\to\\file"
1) store it somewhere in the WEB-INF directory
2) get the real path of the WEB-INF directory via ServletContext.getRealPath
3) in your code, create a Properties object, and call its load method with the file name

There are other ways, but this is the quickest to understand and implement.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!