file input

i am trying to read a file where the numeric values use the formats of the DataInputStream and DataOutputStream classes.  the text values contain 8 bit chars, using 8 bit us ascii encoding.  so i read the numeric values using the DataInputStream class, but then when i reach a text value, what should i use to read it?
abstractionzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Just read()
0
CEHJCommented:
But how do you know where one starts and the other ends?
0
abstractionzAuthor Commented:
i know the length in bytes of the text
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

CEHJCommented:
In that case:

read(byte[], int off, int len)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
abstractionzAuthor Commented:
so when i want to output the text, i have to loop though the byte array?
0
CEHJCommented:
If you've read it into the byte array, just do

System.out.println(new String(byteArray));
0
abstractionzAuthor Commented:
and how do i get the offset of the start of the text?
0
abstractionzAuthor Commented:
say i've read in some numeric values then i come across text, is there anyway to "say" start here instead of specifying an offset?
0
objectsCommented:
> System.out.println(new String(byteArray));

Assuming your platforms default encoding is us ascii ;)
0
CEHJCommented:
If you mean, for the purposes of the function call, 'off' is just where it goes in the byte array. In your case 0
0
CEHJCommented:
>>say i've read in some numeric values then i come across text, is there anyway to "say" start here instead of specifying an offset?

I think you're confused about what 'off' is (see above). If you've read a couple of ints and you know your String is 4 bytes long, simply do

read(yourArray, 0, yourArray.length)
0
objectsCommented:
Should code to read should be:

byte[] buf = new byte[len];
int remaining = len;
while (remaining>0)
{
  remaining -= in.read(buf, buf.length-remaining, remaining);
}

Or better still:

in.readFully(buf);
0
CEHJCommented:
That would only be appropriate if the 'string' data occurred at the end of the stream, otherwise you'd read into numeric stuff. If it *is* at the end, you should do readUTF
0
objectsCommented:
> That would only be appropriate if the 'string' data occurred at the end of the stream

Why?? The buf is the same size as the text being read.
0
CEHJCommented:
>>Why??

Yes, sorry - read you code too quickly. readFully(byte[]) is the simplest
0
objectsCommented:
And if the byte array wasn't the same size then you shouldn't be using:

System.out.println(new String(byteArray));

;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.