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

create file in servlet

 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

How can I create a file in a servlet? The code below is in my doPost method, but it doesn't work.

File file = new File("testing.txt");
file.createNewFile();

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it cause your computer to explode? What does "doesn't work" mean?

That asked, you're not specifying any file path.
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Does it cause your computer to explode? What does "doesn't work" mean?

That asked, you're not specifying any file path.

No, it didn't explode yet. =)
It works using absolute path "....../jfreechartweb/images/testing.txt".
I'll try to make it work using relative path (if it's possible, of course =)

Thank you!
 
Paul Clapham
Sheriff
Pie
Posts: 20750
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leandro Coutinho wrote:I'll try to make it work using relative path (if it's possible, of course =)


Of course a relative path (like the one you tried) is relative to the current working directory. It would be easy to find out what that is for your servlet container, but it might be different for some other servlet container. So you shouldn't really rely on finding out what it is. Not to mention that putting files in that directory might be pointless -- I don't know why you would want to do it anyway.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... in other words, don't use relative paths in web apps. Use absolute paths that you created using ServletContext.getRealPath.
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you folks! I just think about this, and it doesn't make sense use relative path in a web application.
Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic