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):
Categories: Java
Java File Handling, Java IO Programs
Comments (0)
Trackbacks (0)
Leave a comment
Trackback




