Solved

file input

Posted on 2003-11-25
16
288 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
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
map related example 6 59
how to see all occupied ports on windows 10 laptop 15 85
mysql jsp example issue 32 75
Java Inheritance super keyword use 8 40
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

730 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