Solved

file input

Posted on 2003-11-25
16
284 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 28
SequenceInputStream example 3 19
jsp error 6 26
What browser will run Java? 7 63
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.

828 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