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

FileWriter  RSS feed

 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PrintWriter hai = new PrintWriter(new FileWriter("/test1/test2/hai.txt"));

Whether this will create hai.txt under /test1/test2/ ???
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, if you have write permission in that directory.
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know for sure, so the simple answer is, if you require a certain behaviour, read:

File paying attention to the mkdirs method.

Then use the appropriate constructors for FileWriter and PrintWriter.

Alternatively, you could just try it.
 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
test1 and test2 directories are there with write permissions. Eventhough hai.txt is not created under test1/test2/
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this appears to be working, i.e. no IOException is thrown, can I suggest that you do something like:



And see if the path is what you expect it should be.
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you getting an exception?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to IO and Streams...
 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i don't have the write permission of directory for creating the file, can i do chmod 777 dirname ? any other option is available thru File / FileWriter Constructors ?
 
Joe Ess
Bartender
Posts: 9428
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only if you own the directory or are root.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!