• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File IO with Servlet

 
Alen Mester
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alen Mester
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The misuse of finally in this code is disturbing.
 
Alen Mester
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys

Let me check it, beause I still have problems with the response
 
Angad Yadav
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();

 
Alen Mester
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4469
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic