aspose file tools*
The moose likes Beginning Java and the fly likes Write to a txt. file, via user input? (java with netbeans) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Write to a txt. file, via user input? (java with netbeans)" Watch "Write to a txt. file, via user input? (java with netbeans)" New topic
Author

Write to a txt. file, via user input? (java with netbeans)

doug salls
Greenhorn

Joined: Feb 28, 2010
Posts: 8
I am working on a read/write program that accesses information from said file..

Reading from it works.

I have been searching for a way to write to a file using using the input from a user.

I have ran into mostly code that is set to auto fill with predefined entries. Any help would be much appreciated.

Any bit of code, example, point in the right direction would be awesome..
I know scanner can read from files but i found nothing on it being able to write to text. files.

thanks.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

When I searched the web for "write text file in Java" I found quite a few examples--are you having a specific issue? What have you tried so far?

Welcome to JavaRanch!
doug salls
Greenhorn

Joined: Feb 28, 2010
Posts: 8
What i have been successful with the inputs to my file that are just pre entered as part of the code.

BufferedWriter bw;
bw = new BufferedWriter(new FileWriter("filepath",true));
bw.write("Hello World!");
bw.close();

What i am not sure how to get around, is a bufferedwriter command for next entry to be written to my file.
Or if there is a better option for taking inputs and writing them to a file.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Write a string variable instead of a string constant?
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
doug salls wrote:
What i am not sure how to get around, is a bufferedwriter command for next entry to be written to my file.

Just use another bw.write(....); command.
You may also write some separators between adjacent chunks of text (could be a new line or tabulation),
like this:

The above code writes three line of text to the file (\n is a 'new line').
doug salls
Greenhorn

Joined: Feb 28, 2010
Posts: 8
Attempting to get the string variable working.. Problem i am running into as now is if the file is currently empty, even with the entry. Assuming i did it right. program crashes on me.


doug salls
Greenhorn

Joined: Feb 28, 2010
Posts: 8
I am not sure why this is giving me so much grief. The arrays i set up for my last program were easy to work with and track.
TO my knowledge you cant pick say line 5 of a text file., and just println that information out. You would need to go through each line before it and once its reached said line you could have it set to print out only that data...

Its much easier to write to the arrays as well.. IMO.... head vs desk==Splat.
doug salls
Greenhorn

Joined: Feb 28, 2010
Posts: 8
Its amazing what a little bit of sleep will do for you.... Figured it out thank you much for the input.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38805
    
  23
Welcome to the Ranch

Please find the Code button; since you are new I shall edit your code so you can see how much better it looks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38805
    
  23
For full marks, you ought to tidy up your imports in that code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Write to a txt. file, via user input? (java with netbeans)