Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing an integer to a file

Status
Not open for further replies.

plook

IS-IT--Management
Apr 1, 2000
33
0
0
Hi there,

I hope everyone here is doing great. I am looking for some hints.

In the class I am making, I have the following code :

if(counter==3)
{
WriteIntByByte(some_integer);
System.out.println("test");
}

And WriteIntByByte is this :
static void WriteIntByByte( int x ) throws IOException
{
int [] mask = { 0xff000000, 0x00ff0000, 0x0000ff00,0x000000ff };
for (int i = 0; i<4; i++) ous.write((mask & x) >>> (24 - 8*i));
}

All what WriteIntByByte does is to take 4 bytes and make one Integer using bitwise operators. Then write it using BufferedOutputStream.

When I do like this :
if(counter==3)
{
WriteIntByByte(some_integer);
System.out.println("test");
}
//That displays "test" on the screen, but does not write anything

When I try this :

WriteIntByByte(some_integer);
if(counter==3)
{
System.out.println("test");
}
//That displays "test" and write to the file.

So, trying to write inside the if does not work, while outside does. Ok fine, so I tried

if(counter>0)
{
WriteIntByByte(some_integer);
System.out.println("test");
}
//That works, prints "test" and write to the file.

But...
if(counter>1)
{
WriteIntByByte(some_integer);
System.out.println("test");
}
//That does not work: only prints "test".

Maybe it's because I don't have a lot of experience in Java, but that seems weird to me. I mean, sometimes it does into the if, runs the second statement, but not the first one.

Any suggestion or help would be greatly appreciated.

Thank you in advance.

Dominic
 
Thanks for your reply stefanwagner,

This is exactly what I needed to add. Thank you !

I did not know I had to have a flush(), I thought a close() was doing the same thing.

Thanks again, I really did not know what was the problem.

Dominic
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top