private static void testWrite1() throws Exception {
FileOutputStream fos = new FileOutputStream("D:/x.txt");
//
FileChannel fc = fos.getChannel();
//
ByteBuffer bf = ByteBuffer.allocate(1024);
bf.putInt(1);
bf.putInt(2);
System.out.println(bf);
//
// ByteBuffer bf = ByteBuffer.wrap(";Hello,java NIO".getBytes());
bf.flip();
System.out.println(bf);
fc.write(bf);
fc.close();
fos.close();
}
Question 1:
written documents in the hair like this:
Question 2:
Why
ByteBuffer bf = ByteBuffer.wrap ("; Hello, java NIO". getBytes ());
this form when you create a ByteBuffer but you have to put bf.flip (); sentence Notes can be written off , or is an empty file ?
What is the secret on the inside, to solve !
------ Solution ---------------------------------------- ----
landlord look Character Encoding the relevant knowledge it.
files only binary data , as to what format the data is stored , up to the landlord to decide.
ByteBuffer is bit-oriented , byte , an integer from 4 bytes.
two integer data is 8 bytes.
read, but also in accordance with the rules of this encoding to read , according to the character encoding to read the rules , then the display will not be an integer.
question two , requiring the landlord to take a closer look ByteBuffer API documentation for the internal operation of the buffer , so naturally understand how the matter stood .
------ Solution ---------------------------------------- ----
write file doing it without the traditional io , nio personally feel should be more suitable for high concurrency network transmission, the file for writing only increase complexity.
------ Solution ---------------------------------------- ----
wood used . . .
------ Solution ---------------------------------------- ----
+1 should always keep in mind : the development of the most appropriate time to achieve your function , rather than for what what .
------ Solution ---------------------------------------- ----
so I chose to use a technology , we must first read is what this technology can do , rather than directly to the use of specific API interface
------ Solution ----- ---------------------------------------
character stream using Writer, use byte streams are not the desired result.
------ Solution ---------------------------------------- ----
nio after the jdk5 no longer updated over , jdk6, 7 rewritten io bottom part is nio technology, except in areas of high concurrency , otherwise no need to tangle nio, sister, brother suggested it < br> ------ For reference only ---------------------------------------
picture is what you personally ? yes I can go into the details of this technique can be . . .
------ For reference only ---------------------------------------
I have to figure out , see this book nio
Thank you .
For the 8th floor , you've exposed the nature of your color . . .
没有评论:
发表评论