Writing into ViewBuffers!! need help
I am using a viewbuffer here to copy the string to a file.
When i copy the string to a byte array and write to a view buffer i get the following error :
WritingFiles.java:65: cannot find symbol
symbol : method put(byte[])
location: class java.nio.CharBuffer
cbuf.put(bytes);
Code Java:
public class WritingFiles {
public static void main(String[] args) {
File afile = new File("E:/java/samples/testing/proverbs.txt");
String[] sayings = { " Indecision maximises flexibility ",
" Only the mediocre are always at their best "
};
FileOutputStream outStream = null;
try
{
outStream = new FileOutputStream (afile,true);
}catch (FileNotFoundException e)
{
e.printStackTrace(System.err);
System.exit(1);
}
FileChannel outChannel = outStream.getChannel();
int maxlength = 0;
for ( String saying : sayings )
if ( maxlength < saying.length())
maxlength = saying.length();
ByteBuffer buf = ByteBuffer.allocate(2*maxlength+4);
try
{
for(String saying : sayings)
{
buf.putInt(saying.length());
CharBuffer cbuf = buf.asCharBuffer();
byte[] bytes = saying.getBytes();
cbuf.put(bytes);
buf.position(buf.position()+2*saying.length()).flip();
outChannel.write(buf);
buf.clear();
}
}
outStream.close();
System.out.println("Proverbs written to file");
}catch(IOException e)
{
e.printStackTrace(System.err);
System.exit(1);
}
System.exit(0);
}
}
Re: Writing into ViewBuffers!! need help
Quote:
WritingFiles.java:65: cannot find symbol
symbol : method put(byte[])
location: class java.nio.CharBuffer
cbuf.put(bytes);
See the java doc for CharBuffer. put() doesn't take any byte[] parameter.
Re: Writing into ViewBuffers!! need help