This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes write to a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "write to a file" Watch "write to a file" New topic
Author

write to a file

Jay Brass
Ranch Hand

Joined: Oct 24, 2000
Posts: 76
Hey there,
If I use this:

how do I put the readChar bit into a file and save it for a rainy day? Or can I? I don't have a gui interface so I can't use getText() I just want what the user types in up until the user presses enter.
Thx in advance
J
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
If you want to capture an entire line of input you might consider wrapping a BufferedReader around your existing reader and using the readLine() method to capture input. Then you can store the input in a string and do with it as you please. That would look something like this:

To write the string to a file, you will need to open an outputStream and a writer to the file and then pass the string to the writer.
Jay Brass
Ranch Hand

Joined: Oct 24, 2000
Posts: 76
Bodie - Dude,
Thanks a heap
It took a whole nite of typing and trying things while downloading as much as possible from napster.
But I finally got it to work. I couldn't however, get the writer bit to work. Here is what my code looks like PLEASE feel free to point out better ways or critism or mercilously mock it as you wish:

It ain't pretty but it works!
again thx
J
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Jay, your code looked pretty good. I made some changes (most were stylistic) and got it to work. The problem that I seem to have with writing to files is that Windows NT doesn't recognize filenames as being relative. I have to specify a full path. If I don't, the file stream doesn't get created. There is probably something you can do with the string to indicate that it is relative to your JVM, but I don't really know what that would be, so I always give a full path and filename.
Anyway! Try this:

I hope this helps.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: write to a file
 
Similar Threads
How to Change the File Name for Each Uploaded Files to the Socket Server?
How to read MAGIC for db-2x2.db
Binary
Searching a file
File path issues while reading an xsl file in java