-
Didn't do it.
Java bites! Er, bytes. Yeah...
Gargh!
Trying to get a basic little app to work in Java - simple: read in file, perform some operations (display, reverse, rename, encrypt). I've got it all working (except for the encrypt part) while reading the file into a CharBuffer, but I'm trying to convert to a ByteBuffer to do the encription...
I've got it loading properly, but the display function always kicks out "0 - Exit" at the end when casting the bytes to chars; it's causing some real problems when reversing the order of bytes in the reverse function...
Anyone know how to troubleshoot this? Is it a matter of eliminating an eof character from the inputstream, or testing the last bytes for a specific value (which, for the life of me, I cannot think of).
If anyone is bored enough and home on a Friday night, my code follows:
public void showFile() throws IOException{
// display contents of file
FileChannel inChannel = (new FileInputStream(fileName)).getChannel();
originalBytes.clear();
// ByteBuffer obj declared earlier
originalBytes.allocate((int)inChannel.size());
while(inChannel.read(originalBytes) != -1){
// read bytes into buffer
}
originalBytes.flip();
inChannel.close();
// now, convert bytes to chars and output
while (originalBytes.hasRemaining()){
System.out.print( (char) (originalBytes.get() & 0xFF) );
}
}
public void reverseFile(){
// reverse the file
originalBytes.rewind();
// declare 2nd buffer
reverseBytes.allocate(originalBytes.limit());
// read first buffer into second in reverse order
for (int i = originalBytes.limit(); i <=0; i--){
reverseBytes.put(i, originalBytes.get());
}
try{
FileChannel outChannel = (new FileOutputStream(fileName)).getChannel();
outChannel.write(reverseBytes);
outChannel.close();
originalBytes.clear();
System.out.println("The file has been reversed. Please select option 2 to view the results.\n");
} catch (IOException ioe){
System.out.println("Write to file failed.");
}
}
Any help appreciated!
Hush child. japangreg can do what he wants. - PAlexC
That was Zen - this is Tao.
-
:O I think this be the wrong spot. You should know by now.
If it isn't, the rules are written wrong.
"For an assassin he...he is pretty nice." ~The Darkness that Comes Before
The Catalyst Corp.
-
Retired SCORM Guru
Hush child. japangreg can do what he wants.
"What really bugs me is that my mom had the audacity to call Flash Kit a bunch of 'inept jack-asses'." - sk8Krog
...and now I have tape all over my face.
-
pablo cruisin'
Originally Posted by PAlexC
Hush child. japangreg can do what he wants.
</zing!>
"Why does it hurt when I pee?" -- F. Zappa |
-
:P I know, noone will pay it attention though.
"For an assassin he...he is pretty nice." ~The Darkness that Comes Before
The Catalyst Corp.
-
Wait- what now?
As a wise gerbick once said.
use a lower case p
"I'd only told them the truth. Was that so selfish? Our integrity sells for so little, but it is all we really have. It is the very last inch of us, but within that inch, we are free."
-
Senior Member
One more reason to not learn Java. I'm fine with OOP, but that looks ridiculous. I hope the NetBeans IDE has code hinting.
-
Just roll with vb.net or Ruby on Rails. Depending on the environment you're programming for. You'll most likely want to kill yourself less.
-
Didn't do it.
Originally Posted by PAlexC
Hush child. japangreg can do what he wants.
That's footer material right there.
Hush child. japangreg can do what he wants. - PAlexC
That was Zen - this is Tao.
-
No!
Did you ever figure this out? It seems like you would want to use originalBytes.getChar() instead of originalBytes.get()
-
Databarnak
... woooo .. see that UFO?
I ask you all to concentrate really hard on the freedom of all being. Its hard not to be very angry it is impossible We have to focus this confusion frustration helplessness feeling into a creative outlet Anger can spawn such amazing creativity through Street art Free art to teach each other know each other a language our evolution Go ahead and break some dumb rules
-
Didn't do it.
Originally Posted by yasunobu13
Did you ever figure this out? It seems like you would want to use originalBytes.getChar() instead of originalBytes.get()
So it would seem, but Java hates me and makes me jump through hoops.
<sheepish grin>
Figured out the "0 - Exit" problem, at least: I forgot to put a line break between the output and my menu (which starts with 0 - Exit, 1 - Select File, etc.) This has got to be one of the worst "You've got to be f-ing kidding me" moments... I just spent 2 days searching the net for my own damn error...
</sheepish grin>
Now all I have to do is fix the reverse function to not write past the limit of the buffer and I'm good... thanks to all for the help, anyway.
*edit*
Got it - and the encrypt function, too. You know, once you remove blinding stupidity from the equation, this isn't all that hard... =^_^=
Last edited by japangreg; 08-16-2007 at 04:08 PM.
Hush child. japangreg can do what he wants. - PAlexC
That was Zen - this is Tao.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|