//******
Import Java.nio.ByteBuffer;
Import Java.nio.channels.FileChannel;
Import Java.io.File;
Import Java.io.FileOutputStream;
public class filechanneldemo01{
public static void Main (String args[]) throws exception{
String info[] = {"Mldn", "Mldnjava", "www.mldn.cn", "www.mldnjava.cn", "Li Xinghua", "Lixinghua"};
File File = new file ("." + File.separator + "OUT.txt");
FileOutputStream output = null;
Output = new FileOutputStream (file);
FileChannel fout = null; Declaring FileChannel objects
Fout = Output.getchannel (); Get the output channel
Bytebuffer buf = bytebuffer.allocate (1024);
for (int i=0;i<info.length;i++) {
Buf.put (Info[i].getbytes ()); The string becomes a byte array and put into the buffer
}
Buf.flip ();
Fout.write (BUF); Contents of output buffers
Fout.close ();
Output.close ();
}
}
//*****
Import Java.nio.ByteBuffer;
Import Java.nio.channels.FileChannel;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.io.FileInputStream;
public class filechanneldemo02{
public static void Main (String args[]) throws exception{
File File1 = new file ("." + File.separator + "Note.txt");
File File2 = new file ("." + File.separator + "Outnote.txt");
FileInputStream input = null;
FileOutputStream output = null;
Output = new FileOutputStream (file2);
input = new FileInputStream (file1);
FileChannel fout = null; Declaring FileChannel objects
FileChannel fin = null; Define the input channel
Fout = Output.getchannel (); Get the output channel
Fin = Input.getchannel (); Get the input channel
Bytebuffer buf = bytebuffer.allocate (1024);
int temp = 0;
while ((Temp=fin.read (BUF))!=-1) {
Buf.flip ();
Fout.write (BUF);
Buf.clear (); Clears the buffer and restores the position of all state variables to the origin.
}
Fin.close ();
Fout.close ();
Input.close ();
Output.close ();
}
}
//*****
Import Java.nio.ByteBuffer;
Import Java.nio.MappedByteBuffer;
Import Java.nio.channels.FileChannel;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.io.FileInputStream;
public class filechanneldemo03{
public static void Main (String args[]) throws exception{
File File = new file ("." + File.separator + "Mldn.txt");
FileInputStream input = null;
input = new FileInputStream (file);
FileChannel fin = null; Define the input channel
Fin = Input.getchannel (); Get the input channel
Mappedbytebuffer MBB = null;
MBB = Fin.map (Filechannel.mapmode.read_only,0,file.length ());
byte data[] = new byte[(int) file.length ()]; Open up space to receive content
int foot = 0;
while (Mbb.hasremaining ()) {
data[foot++] = Mbb.get (); Reading data
}
System.out.println (new String (data)); Output content
Fin.close ();
Input.close ();
}
}
//****
Import Java.nio.ByteBuffer;
Import Java.nio.channels.FileChannel;
Import Java.io.File;
Import Java.io.FileOutputStream;
public class filechanneldemo01{
public static void Main (String args[]) throws exception{
String info[] = {"Mldn", "Mldnjava", "www.mldn.cn", "www.mldnjava.cn", "Li Xinghua", "Lixinghua"};
File File = new file ("." + File.separator + "OUT.txt");
FileOutputStream output = null;
Output = new FileOutputStream (file);
FileChannel fout = null; Declaring FileChannel objects
Fout = Output.getchannel (); Get the output channel
Bytebuffer buf = bytebuffer.allocate (1024);
for (int i=0;i<info.length;i++) {
Buf.put (Info[i].getbytes ()); The string becomes a byte array and put into the buffer
}
Buf.flip ();
Fout.write (BUF); Contents of output buffers
Fout.close ();
Output.close ();
}
}
Java New IO "_ Channel" \ Code