• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 550
  • Last Modified:

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.
0
MarkLoveExEx
Asked:
MarkLoveExEx
  • 4
  • 3
  • 2
1 Solution
 
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
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
CEHJCommented:
>>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
 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now