Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

file path

 
Hong Yi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a question about file location. when I give physical location (for example c:\test\file.txt) for my file path on my local machine. The file can be found and read.
My question is: does it work in DEV or PROD environment? if not, what is the correct path I should give?

Thanks
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hong Yi wrote:My question is: does it work in DEV or PROD environment?

If you have your files stored in a known location like /mine/properties, this does work. An alternative is to add the directory to your classpath and let Java read them as a resource. If your files are under a server, you have to do this.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DEV or PROD environment in what sense? Is this a web application?
 
Hong Yi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DEV or PROD is deployed environment. The application is web application. and static files like html will be deployed to web server and dynamic files like jsp will be deployed to application sever.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on how you want to do it, then. Direct client access, no. You'd have to read via absolute path or as Jeanne mentioned and write the response.
 
Hong Yi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David and Jeanne. I use the following code:

InputStream is = getClass().getClassLoader().getResourceAsInputStream("fileName") ;

it works on localhost and DEV environment without giving a physical path of the file.

but I still have two questions:

1> how does it find the file?

2>do I need to provide "/" before the fileName like "/fileName"? and why does it work without "/"

Thanks
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It finds the file by looking on the classpath.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic