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

kathywargo
kathywargo used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mick BarryJava Developer
Top Expert 2010

Commented:
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 ) ;

Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Commented:
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.
Top Expert 2016

Commented:
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

Author

Commented:
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);
                }



Top Expert 2016

Commented:
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...
I have decided to use a file in and write to a file.  Then use the file as input to another.   I knew that would work, but I just was hoping there was something elsel
>>I knew that would work, but I just was hoping there was something elsel

did you check comment ID:25659654?

Author

Commented:
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.
Top Expert 2016

Commented:
>>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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial