Solved

file input

Posted on 2003-11-25
16
263 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now