Results 1 to 6 of 6
Thread: write nio.CharBuffer to file
- 10-18-2008, 09:02 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 5
- Rep Power
- 0
- 10-18-2008, 09:13 PM #2
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.
- 10-18-2008, 09:37 PM #3
Member
- Join Date
- Oct 2008
- Posts
- 5
- Rep Power
- 0
Examples like that:
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 :/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()); } } /* */
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:)
- 10-19-2008, 03:16 PM #4
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.
- 10-19-2008, 05:50 PM #5
Member
- Join Date
- Oct 2008
- Posts
- 5
- Rep Power
- 0
- 10-19-2008, 08:20 PM #6
Similar Threads
-
how to write onto a file
By mirage_87 in forum New To JavaReplies: 6Last Post: 09-08-2009, 03:54 PM -
Write to file
By esadeghi in forum Advanced JavaReplies: 1Last Post: 05-21-2008, 01:13 PM -
File Write Error
By vikain in forum Advanced JavaReplies: 5Last Post: 01-02-2008, 04:38 AM -
Write unicode into file
By vata2999 in forum New To JavaReplies: 1Last Post: 08-08-2007, 03:04 PM -
Help with write file in java
By mathias in forum New To JavaReplies: 1Last Post: 07-31-2007, 06:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks