• Post Reply Bookmark Topic Watch Topic
  • New Topic

RE:queries in files  RSS feed

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



1)how to delete the contents of the files?

randomaccessfile rm=new randomaccessfile("rw"); is the answer?

please give me the right answer?


2)how to change the current working directory?


can we use chdir,cd or there is no method to change it directly?


please give me the lines of code for the above 2 questions
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vidya Bhargavi wrote:. . .
randomaccessfile rm=new randomaccessfile("rw"); is the answer?. . .
Unless you have created your own class, that won't even compile, so No.
Have you read the API specifications? Or the Java™ Tutorials?
2)how to change the current working directory?


can we use chdir,cd or there is no method to change it directly?
Why on earth would you want to change the working directory?
 
Sheriff
Posts: 21136
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vidya Bhargavi wrote:1)how to delete the contents of the files?

randomaccessfile rm=new randomaccessfile("rw"); is the answer?

No it isn't. That will open the file for reading and writing but will not change its contents.

I know two ways to do this:
1) use FileChannel.truncate to set the size to 0.
2) use a FileOutputStream and don't write anything to it.

2)how to change the current working directory?

can we use chdir,cd or there is no method to change it directly?

Try using setProperty("user.dir", "new path");
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!