File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
doug salls
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a string variable instead of a string constant?
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its amazing what a little bit of sleep will do for you.... Figured it out thank you much for the input.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For full marks, you ought to tidy up your imports in that code.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic