Link to home
Start Free TrialLog in
Avatar of kathywargo
kathywargo

asked on

How can I use a DataOutPutStream as an DataInputStream in a different function?

I have a function which reads a file as DataInputStream (dataIs). I then do the following
             br = new BufferedReader(new InputStreamReader(dataIs));
             String strLine;
              //Read File Line By Line
               while ((strLine = br.readLine()) != null)   {
                   if (strLine = "testOut")
                  dataOut.writeBytes(strLine);
                }

I want to return dataOut as a DataInput Stream, which will be used in another function.  
           
     something like  ==>     return (DataInputStream)dataOut;

Sounds like there should be some way to do this, easily.  

All help is welcome.  Obviously I'm  new to java.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

yes thats fine
just declare that your method retuns a DataOutputStream (and don't close() it before returning it
you can't retrun DataOutputStream as DataInputStream...

if you need to return DataInputStram, construct a ByteArrayInputStream and wrap it using DataInputStream and then return it!
for e.g., here is how you convert a string in to BIS,

String tmp = "abcdefghijklmnopqrstuvwxyz";
byte b[] = tmp.getBytes();
ByteArrayInputStream bis = new ByteArrayInputStream(b);

to create DataInputStream

DataInputStream dis = new DataInputStream( bis ) ;

You should describe what you want to do and how the output stream is created. For example, you stream is writing to "something" (e.g. file), you can then create an input stream from that object. If you are writing to a stream you don't have access to, you may want to add an extra stream to an accessible object.
Sounds like you need to do some reorganization. For one thing, you're using a DataOutputStream to write merely lines of text, which isn't appropriate. You also don't want to couple the two methods. If the second is foo(DataInputStream in) then you should simply open the file again. An alternative would be to use a BufferedInputStream and call reset on it, but again that would cause coupling.

We need more details
Avatar of kathywargo
kathywargo

ASKER

Okay I am throughly confused.

First of all I have to pass a dataInputstream to call another function.  That's a given, I can't change the call to the other function.
I need to open a file, then read the data, and write datafrom dataIn to another stream (dataOut) based on a certain condition.  Now I have to pass dataOut to another function as a dataInputStream.

I thought since I was doing all this in memory, why do I have to write out to a file.  

Please explain how to convert dataOuputStream to dataInputStream.  Perhaps dataOutputStream should be something else like a BufferedWriter, but again my final result must be an DataInputStream.

I open a file, convert it to   br = new BufferedReader(new InputStreamReader(dataIs));
             String strLine;
              //Read File Line By Line
               while ((strLine = br.readLine()) != null)   {
                   if (strLine = "testOut")
                  dataOut.writeBytes(strLine);
                }



Try something like the following
    public DataInputStream foo(DataOutputStream dataOut) throws IOException {
	File f = new File("x");
	BufferedInputStream bin = new BufferedInputStream(new FileInputStream(f));
	bin.mark((int)f.length());
	br = new BufferedReader(new InputStreamReader(bin));
	String strLine;
	//Read File Line By Line
	while ((strLine = br.readLine()) != null)   {
	    if (strLine = "testOut")
		dataOut.writeBytes(strLine);
	}
	bin.reset(0);
	return new DataInputStream(bin);
    }

Open in new window

>>Please explain how to convert dataOuputStream to dataInputStream

check comment ID:25659654

and its not a good practice to return the file input stream as somebody might update the file...
ASKER CERTIFIED SOLUTION
Avatar of kathywargo
kathywargo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>I knew that would work, but I just was hoping there was something elsel

did you check comment ID:25659654?
In response to the experts comment 25661275, I am looking at the code and it doesn't seem to be what I wanted.  I want the results of dataOut; in this example it would be lines of "TestOut" be passed back as a DataInputStream.


public DataInputStream foo(DataOutputStream dataOut) throws IOException {
      File f = new File("x");
      BufferedInputStream bin = new BufferedInputStream(new FileInputStream(f));
      bin.mark((int)f.length());
      br = new BufferedReader(new InputStreamReader(bin));
      String strLine;
      //Read File Line By Line
      while ((strLine = br.readLine()) != null)   {
          if (strLine = "testOut")
            dataOut.writeBytes(strLine);
      }
      bin.reset(0);
      return new DataInputStream(bin);
    }
>>I want the results of dataOut;

you have told you want datainputstream which you can't get directly from dataoutputstream but have to create it. thats what the two comments 25659654 and 25661275 talks about which is far efficient than wrting to another file. with 25661275, there is a good possibilty that file get updated by someone but using bytearrayinputstream would relieve from that hassle.
>>I want the results of dataOut; in this example it would be lines of "TestOut" be passed back as a DataInputStream.

Yes, sorry - i realised that later