This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing data to a file  RSS feed

 
Arjun Murthy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to write some data to a properties file. The file is present in a package like "com.abc.files"

When i try the following:
BufferredWriter write=new BufferredWriter(new FileWriter("com/abc/files/MyFile.properties"));

I get the following error:
"The system cannot find the path specified."

I know that by giving the full path of the file i wont get the error but is there any way to work around this?
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Java Ranch Arjun!

File paths are taken as relative to the working directory. If you are looking for a file on the classpath, you should look into ClassLoader.getResourceAsStream(). That will only work though if the properties file is in a directory that's part of your classpath, because you can't write to files that in a jar.

 
Arjun Murthy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The file i intend to write to is not in a jar. It is just present in a separate package. Is there any way to write to the file by giving a path relative to the package where the java class is present from which i am doing the writing?
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't recommend following this path. The classpath is meant for reading from, not writing to. You can update a properties file in the classpath, but that's all you really want to do. OK, with that warning, you can do something like:



 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which will probably fail if the class is inside a JAR file.
 
Arjun Murthy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the help Greg.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!