?
Solved

file input

Posted on 2003-11-25
16
Medium Priority
?
295 Views
Last Modified: 2010-03-31
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?
0
Comment
Question by:abstractionz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 4
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9820447
Just read()
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9820451
But how do you know where one starts and the other ends?
0
 

Author Comment

by:abstractionz
ID: 9820470
i know the length in bytes of the text
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 9820480
In that case:

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

Author Comment

by:abstractionz
ID: 9820490
so when i want to output the text, i have to loop though the byte array?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9820499
If you've read it into the byte array, just do

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

Author Comment

by:abstractionz
ID: 9820510
and how do i get the offset of the start of the text?
0
 

Author Comment

by:abstractionz
ID: 9820543
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
 
LVL 92

Expert Comment

by:objects
ID: 9820550
> System.out.println(new String(byteArray));

Assuming your platforms default encoding is us ascii ;)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9820559
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9820568
>>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
 
LVL 92

Expert Comment

by:objects
ID: 9820579
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9820599
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
 
LVL 92

Expert Comment

by:objects
ID: 9820614
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9820650
>>Why??

Yes, sorry - read you code too quickly. readFully(byte[]) is the simplest
0
 
LVL 92

Expert Comment

by:objects
ID: 9820676
And if the byte array wasn't the same size then you shouldn't be using:

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

;-)
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question