cancel
Showing results for 
Search instead for 
Did you mean: 

How to "wrap" FileOutPutStream to write bytes?

Former Member
0 Kudos

Hello,

I am using an object that has this method:

write(OutPutStream os)

I am FileOutPutStream in order to use this method.

Due to language problems I would like to write the output in bytes rather than chars. How can I "wrap" the FileOutPutStream and still pass the write() method an instance of type OutPutStream ?

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi,

To read or write a file in Java a FileInputStream or FileOutputStream has to be created.The simplest constructors for these takes the filename as a string.These objects can read or write bytes.

Create a FileOutputStream, which can write bytes to the file. Then wrap a PrintWriter around that FileOutputStream by constructing the PrintWriter with the FileOutputStream as the sole argument to the constructor. The PrintWriter then has a println() method which takes Strings and converts them into bytes, which it passes to the FileOutputStream. To read a text file, wrap a BufferedReader around an InputStreamReader wrapped around a FileInputStream.

The example below, which opens a text file and a data file (binary file), writes to them both, closes them, reopens them for reading and reads from them both:

Sample Code:

import java.io.*;

import java.lang.*;

public class FileTest

{

public static void main( String[] args )

{

try

{

//prepare console input

InputStreamReader consoleReader = new InputStreamReader(System.in);

BufferedReader consoleInput = new BufferedReader(consoleReader);

//prepare text file output

FileOutputStream textFileOut = new FileOutputStream("file.txt");

PrintWriter textFileWriter = new PrintWriter(textFileOut);

//prepare data output

FileOutputStream binaryFileOut = new FileOutputStream("file.dat");

DataOutputStream dataFileOut = new DataOutputStream(binaryFileOut);

System.out.println("Enter some text to print to text file " +

"- end with CTRL-D on empty line");

String line;

while ( (line = consoleInput.readLine()) != null )

{

textFileWriter.println(line);

}

textFileWriter.close(); // close the file

System.out.println("\nNow we'll write some data to a binary file...");

System.out.print("Give me a real number: ");

line = consoleInput.readLine(); // get the input

double d = Double.parseDouble( line ); // convert to double

dataFileOut.writeDouble(d); // write to file

System.out.print("Now give me an integer: ");

line = consoleInput.readLine(); // get the input

int i = Integer.parseInt( line ); // convert to int

dataFileOut.writeInt(i); // write to file

dataFileOut.flush(); // make sure its written to file

binaryFileOut.close(); // close the file

//now, we'll read the files:

//prepare text file input

FileInputStream textFileIn = new FileInputStream("file.txt");

InputStreamReader textFileReader = new InputStreamReader(textFileIn);

BufferedReader textFileLineReader = new BufferedReader(textFileReader);

//prepare data file input

FileInputStream binaryFileIn = new FileInputStream("file.dat");

DataInputStream dataFileIn = new DataInputStream(binaryFileIn);

System.out.println("\nReading from text file:");

while ( (line = textFileLineReader.readLine()) != null)

{

System.out.println(line);

}

System.out.println("\nReading from data file:");

System.out.println("double: "+dataFileIn.readDouble());

System.out.println(" int: "+dataFileIn.readInt());

}

catch(IOException e)

{

System.out.println("ERROR: IO Exception");

}

catch(NumberFormatException e)

{

System.out.println("ERROR: you entered a bad number");

}

catch(FileNotFoundException e)

{

System.out.println("ERROR: couldn't read file...?");

}

}

}

Hope this helps.

Regards,

Pooja.

Former Member
0 Kudos

10X Pooja for your kind help.

Former Member
0 Kudos

If you have character set conversion problems look at

java.nio.* package in java 1.4

And I told you to look at the CONSTRUCTOR...

OutputStream myos = new OutputStream(fileoutputstream);

Enjoy

Message was edited by: F.J. Brandelik

Take a course covering basic java.io

Former Member
0 Kudos

Dear F.J.

As far as I know OutputStream doesn't act in bytecode, and I don't mean it's write() method, I mean if I pass it to a 3rd part's write() metod...

Roy

Former Member
0 Kudos

OK so we're talking about 3 possibilities

a) you mean a serialized object => see ObjectOutputStream...

b) you mean character encoding ... see package java.nio.* built on java.io.*....

c) you really mean java byte code as in class.

For this you need to look at a class loader....

See 2 ways to instanciate a class by looking at java.security.Security and java.security.Provider and java.security.Cipher....

Enjoy

Former Member
0 Kudos

Check out the APIs in java.io.*

and more specifically the OutputStream and FileOutputStream classes.

You'll learn more than if I just told you....

This is basic java stuff you need to understand...

Check as well the constructors and subclassing ...

Enjoy

Former Member
0 Kudos

Hey F.J.,

I did that already but couldn't find any class which inherits from OutPutStream and can "wrap" FileOutputStream with bytes. All I know is OutPutStreamReader but that won't satisfy the method I'm passing it as a parameter....