• 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

File IO with Servlet

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 3
Mac OS X Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys

Let me check it, beause I still have problems with the response
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28239
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic