Solved

file input

Posted on 2003-11-25
16
294 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 86

Accepted Solution

by:
CEHJ earned 125 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

729 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