aspose file tools*
The moose likes Servlets and the fly likes create file in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "create file in servlet" Watch "create file in servlet" New topic
Author

create file in servlet

Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
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

Joined: Jan 10, 2002
Posts: 61606
    
  67

Does it cause your computer to explode? What does "doesn't work" mean?

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
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
Bartender

Joined: Oct 14, 2005
Posts: 18882
    
    8

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
Marshal

Joined: Mar 22, 2005
Posts: 42596
    
  65
... in other words, don't use relative paths in web apps. Use absolute paths that you created using ServletContext.getRealPath.


Ping & DNS - my free Android networking tools app
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
Thank you folks! I just think about this, and it doesn't make sense use relative path in a web application.
Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: create file in servlet