on 06-26-2005 12:56 PM
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 ?
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
83 | |
10 | |
10 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.