Io one kind of flow:
package com.cxy.ssp.Automic; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; import java.util.concurrent.atomic.AtomicStampedReference; //通过nio实现文件io public class Demo3 { public static void main(String[] args) throws Exception{ //1 创建输出流 FileOutputStream fileOutputStream = new FileOutputStream("bas.txt");
// Construction channel FileChannel Channel = fileOutputStream.getChannel ();
// Create buffer the ByteBuffer Buffer = ByteBuffer.allocate ( 1024 ); // String STR = " Hello World " ;
// the data into the buffer buffer.put ( str.getBytes ()); the try {
// tag buffer needs to be emptied, then operation buffer.flip ();
// write the contents of the channel channel.write (buffer); } the catch (IOException E) { E .printStackTrace (); } fileOutputStream.close (); } }
com.cxy.ssp.Automic Package; Import a java.io.FileInputStream; Import java.nio.ByteBuffer,; Import the java.nio.channels.FileChannel; public class Demo4 { public static void main (String [] args) throws Exception { / / first build the input stream, the FileInputStream FileInputStream = new new the FileInputStream ( " bas.txt " ); // flow through the acquisition channel FileChannel channel = fileInputStream.getChannel (); // prepare a cache area of the ByteBuffer buffer = ByteBuffer.allocate ( 1024 ); //Reading data from the channel inside. Byte data channel.read (Buffer); // print the contents of the System. OUT .println ( new new String (buffer.array ())); // Close fileInputStream.close (); } }
package com.cxy.ssp.Automic; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.channels.FileChannel; public class Demo5 { public static void main(String[] args) throws Exception { FileInputStream fileInputStream = new FileInputStream("bas.txt"); FileOutputStream fileOutputStream = new FileOutputStream("b.txt"); FileChannel channel = fileInputStream.getChannel(); FileChannel channel1 = fileOutputStream.getChannel(); channel1.transferFrom(channel,0,channel.size()); channel1.close(); channel.close(); } }
Ideas: first constructed input or output stream, and then output or input flow setup channel, channle
Create a buffer, operates operation, the channel buffer zone
The above code Summary:
1 input and output is in terms with your computer, computer output to the accident, is the output, the computer is input
2 output stream of data bytes needed to put inside the buffer,
3 input streams: the data need not be put inside the buffer, then only the channel from which data can be read