Java New IO "_ Channel" \ Code

Source: Internet
Author: User

//******

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.