Advertisements
Home > Java > Java Program to write the contents onto a ByteArrayOutputStream and from there write the contents on a file, and on another byte array – Q37

Java Program to write the contents onto a ByteArrayOutputStream and from there write the contents on a file, and on another byte array – Q37


Q37: Java Program to write the contents onto a ByteArrayOutputStream and from there write the contents on a file, and on another byte array.

import java.io.*;
import javax.swing.*;

class B_a_o_s{

	public static void main(String args[]) throws IOException{
		String s = JOptionPane.showInputDialog("Enter a Sentence: 
");
		ByteArrayOutputStream f = new ByteArrayOutputStream();
		byte buff[] = s.getBytes();
		
		f.write(buff);
		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("\n To an output Stream");
		
		OutputStream f2 = new FileOutputStream("test.txt");
		f.writeTo(f2);
		f2.close();
		f.reset();
		for(int i=0; i<3; i++)
			f.write('X');
		System.out.println(f.toString());
	}
}

… from College notes (BCA/MCA assignments):


Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: