BufferedWriter & BufferedReader

I have a Java program where I use BufferedWriter to output to a File line by line (bw.write("mystring" + "\n"). Then, I use a BufferedReader to read that file line by line (br.readLine()).
I want to rewrite my program to eliminate the need to write to / read from an external file. Would would I use?  BufferedOutputStream? Some sample code would be appreciated.
MarkLoveExExAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
>>This shows how to write to byte array :

You don't need all that. For the least possible changes to your code, just change BufferedWriter to StringWriter and BufferedReader to String Reader
0
 
for_yanCommented:
you can use ByteArray  streams
0
 
CEHJCommented:
>>I have a Java program where I use BufferedWriter to output to a File line by line (bw.write("mystring" + "\n").

You should be using a PrintWriter when writing to file. If you want to eliminate the file thing altogether, you'd be much better off using a string list

http://technojeeves.com/joomla/index.php/free/74-string-list
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
for_yanCommented:

This shows how to write to byte array :
// Demonstrate ByteArrayOutputStream.
import java.io.*;
class ByteArrayOutputStreamDemo {
public static void main(String args[]) throws IOException {
ByteArrayOutputStream f = new ByteArrayOutputStream();
String s = "This should end up in the array";
byte buf[] = s.getBytes();
f.write(buf);
System.out.println("Buffer as a string");
System.out.println(f.toString());
System.out.println("Into array");
byte b[] = f.toByteArray();
for (int i=0; i<b.length; i++) {
System.out.print((char) b[i]);
}
System.out.println("\\nTo an OutputStream()");
OutputStream f2 = new FileOutputStream("test.txt");
f.writeTo(f2);
f2.close();
System.out.println("Doing a reset");
f.reset();
for (int i=0; i<3; i++)
f.write('X');
System.out.println(f.toString());
}
}

Open in new window

0
 
for_yanCommented:
This shwos how to read from byte array
combine the two - and you don't need files

import java.io.*;
class ByteArrayInputStreamReset {
public static void main(String args[]) throws IOException {
String tmp = "abc";
byte b[] = tmp.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(b);
for (int i=0; i<2; i++) {
int c;
while ((c = in.read()) != -1) {
if (i == 0) {
System.out.print((char) c);
} else {
System.out.print(Character.toUpperCase((char) c));
}
}
System.out.println();
in.reset();
}
}
}

Open in new window



these aexamples are form :
http://www.java-samples.com/showtutorial.php?tutorialid=388

http://www.java-samples.com/showtutorial.php?tutorialid=387
0
 
MarkLoveExExAuthor Commented:
CEHJ,

At this point, I've done something like this:
StringWriter sw = new StringWriter();
sw.write("something" + "\n");
sw.write("something else" + "\n");

How would I use StringReader to read / process this **one line at a time**?

Mark
0
 
CEHJCommented:
As i mentioned, use PrintWriter. Also, reading is simplifiable using Scanner
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
out.println("Line one");

...

Scanner in = new Scanner(sw.toString());
while(in.hasNextLine()) {
    System.out.println(in.nextLine());
}

Open in new window

0
 
MarkLoveExExAuthor Commented:
CEHJ,

I think I figured out what works for me. I used a BufferedReader in conjunction with the StringWriter object, like this:

BufferedReader br = new BufferedReader(new StringReader(sw.toString()));

Amazingly, it worked. And, my code changed very little.

Mark
0
 
CEHJCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.