Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Writing into ViewBuffers!! need help

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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);


    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);
     
        }
    }


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Writing into ViewBuffers!! need help

    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.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Writing into ViewBuffers!! need help

    Thanks buddy

Similar Threads

  1. Applet Not Writing to FTP
    By jtd200 in forum Java Networking
    Replies: 9
    Last Post: January 13th, 2012, 12:25 PM
  2. Writing to DTD Document
    By DanG1775 in forum Java SE APIs
    Replies: 4
    Last Post: December 12th, 2011, 04:41 PM
  3. writing to an output...
    By lairel in forum Object Oriented Programming
    Replies: 4
    Last Post: March 1st, 2011, 04:27 AM
  4. writing to JEditorPane
    By nasi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 8th, 2010, 09:23 PM
  5. I need help writing this program
    By kev2000 in forum Algorithms & Recursion
    Replies: 5
    Last Post: June 4th, 2009, 03:14 AM