Java question.

April 30, 2008 02:34AM
Does anyone else think its stupid that there's no compiler switch to allow for single byte chars? The fact that this statement doesn't work is lame:

<pre>
File f = new File("myfile.dat");

if(f.exists())
{
FileInputStream s = new FileInputStream(f);
byte[] buffer = new byte[f.size()];

int read = -1;
int offset = 0;
while((read = s.read(buffer, offset, buffer.length - offset) != -1)
{
offset += read;
}

s.close();

String str = new String(buffer);
buffer = null;

int beginIndex = str.indexOf(myByteSignature);
int endIndex = str.indexOf(myByteSignature2, beginIndex);

str = str.substring(beginIndex, endIndex);

FileWriter fw = new FileWriter("myoutput.dat");
fw.write(str);
fw.flush();
fw.close();
}
</pre>

To do simple binary file interaction I had to create a class called "ByteString" that was backed by a byte array instead of a normal String.
Subject Author Posted

Java question.

Death_Claw April 30, 2008 02:34AM

As an aside

Rade April 30, 2008 03:14PM

I get warm and fuzzies from finding the most efficient solution.

Death_Claw May 01, 2008 04:37AM

Depends on who's defining 'efficient'

Rade May 01, 2008 05:06AM

Its all relative. I try not to reinvent the wheel when its 80 hours of work...30 minutes I can live with. n/t

Death_Claw May 01, 2008 06:49AM

Re: Java question.

Isildur(VIP) April 30, 2008 05:07AM

Basically...

Death_Claw April 30, 2008 05:32AM

Re: Basically...

Isildur(VIP) April 30, 2008 07:05AM

Re: Basically...

Death_Claw April 30, 2008 07:21AM

Re: Basically...

Isildur(VIP) April 30, 2008 08:41PM

Yeah, I think you have a handle on it. Just forget about thinking of a java char as a C char and you're fine.

Jib April 30, 2008 05:14PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 85
Record Number of Users: 3 May 21, 2024
Record Number of Guests: 258 May 03, 2024