japangreg
08-10-2007, 09:45 PM
Hi, all.
Quick problem I'm having with a project - I'm trying to read a file into a ByteBuffer and have the bytes cast into chars for output. When this occurs, I'm getting a "0- exit" at the end of my string of chars. Another function that reverses the buffer chokes on this, although all code functioned correctly when I first wrote it using CharBuffer and char[] to reverse.
If someone could please take a look below and let me know what might be amiss. Thanks!
public void showFile() throws IOException{
// display contents of file
FileChannel inChannel = (new FileInputStream(fileName)).getChannel();
originalBytes.clear();
// ByteBuffer obj initiated 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.");
}
}
Quick problem I'm having with a project - I'm trying to read a file into a ByteBuffer and have the bytes cast into chars for output. When this occurs, I'm getting a "0- exit" at the end of my string of chars. Another function that reverses the buffer chokes on this, although all code functioned correctly when I first wrote it using CharBuffer and char[] to reverse.
If someone could please take a look below and let me know what might be amiss. Thanks!
public void showFile() throws IOException{
// display contents of file
FileChannel inChannel = (new FileInputStream(fileName)).getChannel();
originalBytes.clear();
// ByteBuffer obj initiated 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.");
}
}