This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes File IO with Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "File IO with Servlet" Watch "File IO with Servlet" New topic
Author

File IO with Servlet

Alen Mester
Greenhorn

Joined: Jul 08, 2011
Posts: 21
Good Night or Good Day for others

My question today is

How is the right way to write an .txt , inside a Servlet.

Here is my jsp



I know the .do extension is a old and easy way but for me is working, for the moment.

Now the important, the servlet.....



I create a filledata.txt in the C:\\, that txt is in white , no words no nothing. Now the idea, After the "SAVE" button inthe JSP , I want to save(write) my name (for example), my age, ......., in that txt. The question is ┬┐HOW? I know that my servlet is wrong. Can anyone help to fix it in the right way
thanks
Tim Quick
Greenhorn

Joined: Jul 22, 2011
Posts: 3

I would suggest using a PrintWriter rather than a DataOutputStream if you want readable text in your file.

PrintWriter _FileWriter = new PrintWriter(new FileOutputStream("c:\\filedata.txt",true));
_FileWriter.println( ID + "," + name + "," + FName + "," + Address + "," + Age + "," + City);
_FileWriter.close();

Regards,
Tim
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61084
    
  66

Tim Quick wrote:I would suggest using a PrintWriter rather than a DataOutputStream

Yes, a DataOutputStream is a poor choice.

However, be sure to use proper naming conventions for variable names: fileWriter rather than _FileWriter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Alen Mester
Greenhorn

Joined: Jul 08, 2011
Posts: 21
Hello and thanks for the help

So after the suggestions you two give me it would be like this, right?

if you asking for the Fname,....., Fwords, is because F start with File so, for me easy to remember, in the jsp

And I have another problem , is not working, after the SAVE button, nothing, a white window shows.

I have only one answer my bad indentation in the servlet



thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61084
    
  66

Not following standard conventions makes code unreasonably hard to read. Start all of your variable names with a lowercase character. Using uppercase is confusing and fewer people will be able to read your code easily.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61084
    
  66

Alen Mester wrote:And I have another problem , is not working, after the SAVE button, nothing, a white window shows.


You are not writing anything to the response. So what else would you expect?
Tim Quick
Greenhorn

Joined: Jul 22, 2011
Posts: 3

Try changing the finally { } clause to something like this:

finally{
out.println("<html><body>");
out.println(ID+": file saved");
out.println("</body></html>");

}

That's the bare bones version of html necessary for your browser to render.

Good luck!
Tim
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61084
    
  66

The misuse of finally in this code is disturbing.
Alen Mester
Greenhorn

Joined: Jul 08, 2011
Posts: 21
Thanks guys

Let me check it, beause I still have problems with the response
Angad Yadav
Greenhorn

Joined: Jul 26, 2011
Posts: 1
Use PrintWriter rather than a DataOutputStream if you want readable text in your file.

PrintWriter _FileWriter = new PrintWriter(new FileOutputStream("c:\\sampleFile.txt",true));
_FileWriter.println( ID + "," + Fname + "," + LName + "," + Address + "," + Age + "," + City);
_FileWriter.close();



Regards,
Angad Yadav
---------------------------------
Work Hard, Be Smart.
Alen Mester
Greenhorn

Joined: Jul 08, 2011
Posts: 21
I still dont get it how is the "response"

For the moment , I resolve a basic error, in the servlet in where "if" is, a key "{" was need it

now after the "save" button the console show this

_________________________________________________________________________________________________

http://localhost:8080/PIntranet/file.do?FID=01&Fname=Alex&FFname=Mester&FAddress=terra&Fage=23&Fcity=Dallas
___________________________________________________________________________________________________

Here is the servlet.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61084
    
  66

Alen Mester wrote:I still dont get it how is the "response"

I do not understand this question.

The misuse if finally is still very disturbing.
Alen Mester
Greenhorn

Joined: Jul 08, 2011
Posts: 21
I will do something about the misuse if finally

The principal matter is that the localhost have the data

http://localhost:8080/PIntranet/file.do?fid=01&fname=Alen&ffname=Mester&faddress=terra&fage=23&fcity=Dallas

I want this data in a text file.

Bear Bibeault say:

You are not writing anything to the response. So what else would you expect?

So how, I do this "response" because i dont know
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Alen Mester wrote:The principal matter is that the localhost have the data

http://localhost:8080/PIntranet/file.do?fid=01&fname=Alen&ffname=Mester&faddress=terra&fage=23&fcity=Dallas

I want this data in a text file.


Why do you keep posting this link? Surely you don't expect us to click on it and see something?
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

without reading all the posts in this thread, i will just post how i did text files in servlets. i created a FileIO helper class.

i wrote this 10 years ago when still new to java so forgive my braces and indenting etc, i do it different now

oops... almost forgot to mention that there were only 2 text files involved and i created them manually. anyway, if this helps great if not oh well


SCJP
Visit my download page
 
wood burning stoves
 
subject: File IO with Servlet