A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
Arduino in Action
this week in the
General Computing
forum!
A special promo:
Enter your blog post or vote on a blogger to be featured in an upcoming Journal
JavaRanch
»
Java Forums
»
Java
»
Beginning Java
Author
I/O Question
Rob Levo
Ranch Hand
Joined: Oct 01, 2000
Posts: 167
posted
Apr 18, 2002 11:06:00
0
I need example code of reading the contents of a file into a
StringBuffer
object.
Thanks to anyone who can provide that example for me.
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
posted
Apr 18, 2002 11:35:00
0
This should do the trick. I even added a line to spit the contents back out to the console so that you could see what was in the
StringBuffer
.
import java.io.FileReader; import java.io.BufferedReader; import java.io.IOException; public class MyReader { public static void main(String[] args) { StringBuffer buff = new StringBuffer(); try { BufferedReader reader = new BufferedReader(new FileReader("myFile.txt")); String readLine = reader.readLine(); while ( readLine != null ) { buff.append(readLine + System.getProperty("line.separator")); readLine = reader.readLine(); } System.out.println(buff.toString()); } catch (IOException e) { System.out.println("Exception caught: " + e); } } }
I hope that helps,
Corey
[ April 18, 2002: Message edited by: Corey McGlone ]
SCJP Tipline, etc.
Rob Levo
Ranch Hand
Joined: Oct 01, 2000
Posts: 167
posted
Apr 18, 2002 17:12:00
0
Thanks Corey, exactly what I needed!!
I agree. Here's the link:
http://aspose.com/file-tools
subject: I/O Question
Similar Threads
What is the plural of Algorithm ?
Mashups: A Java Thing?
How do you print a character a random number of times?
Sub class name- qualify clause
System.err or System.out
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter