• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet and IO - help needed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, obviously I am pretty new in servlets and I have a serious problem.
I want to write the data retrieved from the user to a txt file.
That's sounds easy right but I have been trying this for the past 4 hours , infact I cant even write anythign to a file thru the servlet please help ...
It works fine in a main method of a decent class when I run it but it just doesnt write to the file when I run the servlet...
here is the doGet() method :


public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try
{
BufferedWriter pw = new BufferedWriter(new FileWriter("memberfile.txt", true));
pw.write("Please work!!");
pw.newLine();
System.out.println("Why isn't it working?");
pw.close();
}
catch(IOException ioe)
{
System.out.println("IOException thrown in writeMember() method");
}

}


Why isn't it writing to the file? is there anything I dont know? ... please help and thanks
 
Luca Bracci
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh nevermind!!
I got it working
I as just looking for it in the wrong place, the file actually is being saved in the bin folder ,,, oh I learned somethign new today.... snap I am one slow and dumb person....
 
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I bet, nop you are not. You figured out solution by yourself. Let me drag you down here for a second. I did same as u did. Text file was saved on bin folder. How can I save text file at my root folder not at bin. Like at /mydev. If you have any clue let me know.
BK

Originally posted by Luca Bracci:
Oh nevermind!!
I got it working
I as just looking for it in the wrong place, the file actually is being saved in the bin folder ,,, oh I learned somethign new today.... snap I am one slow and dumb person....


 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't you just specify the absolute directory?
BufferedWriter pw = new BufferedWriter(new FileWriter("YourRootDir\memberfile.txt", true));

Bosun
 
Saloon Keeper
Posts: 27808
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you give a relative path, there's presently no guarantee WHERE it will go, or even it if will go out to the same directory consistently. A good alternative is to specify the root of your intended directory as an application parameter so you don't have to hard-code it into the program/JSP.
BTW, I always recommend using the Unix form of filenames in Java. Java handles them in an OS-independent way, and you won't get zapped when the compiler turns "YourRootDir\memberfile.txt" into "YourRootDirmemberfile.txt" because the backslash is an ESCAPE character!
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMHO there are just two clean ways to access the filesystem in a web-application:
  • Relative to the web-app root. The call getServletContext().getRealPath(relativePath) will return the absolute path for a resource - for instance, getServletContext().getRealPath("WEB-INF/web.xml") will give you the location of the "web.xml" file. The problem with this approach is, some application servers run the entire web-app from a war or a database, not from the filesystem, and will actually return null. If you just need need read-only access, use getServletContext().getResource(relativePath) or getServletContext().getResourceAsStream(relativePath).
  • In a location given by a configurable parameter. The most natural means of configuration is a servlet or application parameter specified in the web.xml file; you can retrieve them using getServletConfig().getInitParameter(name) and getServletContext().getInitParameter(name) respectively.
  • Arguably, access relative to the user's home directory (System.getProperty("user.home")) should be included in the list; I'm not fond of it because it opens the possiblity of conflicts between web-applications. HTH,
    - Peter

    [This message has been edited by Peter den Haan (edited October 17, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic