Results 1 to 6 of 6
  1. #1
    Beju is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default write nio.CharBuffer to file

    Hello, I am completely new to Java, so here goes my stupid question:) How do I write a nio.CharBuffer to a file?? I saw some examples with ByteBuffer inolved, but it's something I cannot fully understand.
    Thanks in advance:)

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default

    What examples have you found?
    Have you searched the API doc for any file writing classes that can take the contents of a CharBuffer as a source? For example what interfaces does CharBuffer implement? Are any of those used as a source for a class that will write?
    You'll probably have to write your own code to read from the CharBuffer and write to the file.

  3. #3
    Beju is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    Examples like that:
    Java Code:
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    
    public class MainClass {
    
      public static void main(String[] args) throws Exception {
        FileChannel fc = new FileOutputStream("data2.txt").getChannel();
        ByteBuffer buff = ByteBuffer.allocate(24); // More than needed
        buff.asCharBuffer().put("Some text");
        fc.write(buff);
        fc.close();
    
        fc = new FileInputStream("data2.txt").getChannel();
        buff.clear();
        fc.read(buff);
        buff.flip();
        System.out.println(buff.asCharBuffer());
    
      }
    }
    /*  */
    Yes I've searched through the documentation, but none of the nio write()'s uses CharBuffer as a source, only ByteBuffers are supported. It's strange for me, they made a whole New IO Library, buffers for multiple data types, but You can write ony ByteBuffer :/
    I wonder why It's so complicated, and also there is no function which returns size of a data structure in bytes, or maybe I didn't found it yet, but in order to make a new ByteBuffer from another [Char,Int,Float, etc...]Buffer You have to multiply the it's size by the sizeof(datatype) during allocation. Isn't it kind of.... stupid?
    But You're right, I can still use a set of loops to write single character at a time:) It's like good old C...:)
    Anyway, thx for interest:)

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default

    After looking at the doc for a while, I was wondering
    How did you get data into a CharBuffer?

    To write contents of a CharBuffer do something like the following to get the data into a ByteBuffer and then use your code from above:
    Java Code:
        String text = "CB text 3";
        CharBuffer cb = CharBuffer.allocate(text.length());
        cb.put(text);
        cb.flip();
    /*
        char[] carray = cb.array();
        System.out.println("hasArray() = " + cb.hasArray() +", length="+ carray.length);
        System.out.print("array=");
        for (int i = 0; i < carray.length; i++) {
          System.out.print(carray[i]);
        }
        System.out.println("");
        System.out.flush();
    */
        Charset cs = Charset.defaultCharset(); // 1.5
        System.out.println("cs= " + cs);  //cs= windows-1252
        CharsetEncoder cse = cs.newEncoder(); 
        System.out.println("cse= " + cse);  //cse= sun.nio.cs.MS1252$Encoder@19821f
        ByteBuffer buff = cse.encode(cb);   // output is 1 byte/char
        System.out.println("buff=" + buff); //buff=java.nio.HeapByteBuffer[pos=0 lim=9 cap=9]
    Last edited by Norm; 10-19-2008 at 03:18 PM.

  5. #5
    Beju is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    After looking at the doc for a while, I was wondering
    How did you get data into a CharBuffer?
    I would happily tell You, but I don't know:) My task is just to write a function which saves the content of a CharBuffer into a file:)

    Thanks for the code, but it's reaaally complicated to me...:confused: Java I/O is very strange....

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default

    Ask the one that gave you the task,how he is putting data into a CharBuffer and also why instead of into a class that has easy to output methods.

    The code I gave shows how to copy a CharBuffer into a ByteBuffer. The code you posted shows how to write a ByteBuffer into a file.

Similar Threads

  1. how to write onto a file
    By mirage_87 in forum New To Java
    Replies: 6
    Last Post: 09-08-2009, 03:54 PM
  2. Write to file
    By esadeghi in forum Advanced Java
    Replies: 1
    Last Post: 05-21-2008, 01:13 PM
  3. File Write Error
    By vikain in forum Advanced Java
    Replies: 5
    Last Post: 01-02-2008, 04:38 AM
  4. Write unicode into file
    By vata2999 in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 03:04 PM
  5. Help with write file in java
    By mathias in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 06:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •