package nio;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
/**
<strong><span style="font-size:24px;color:#ff0000;"> * 实现功能,从一个文件读取数据输出到另一个文件</span></strong>
* @author Administrator
*
*/
public class Test2 {
public static void main(String[] args) {
try {
FileInputStream fin = new FileInputStream("F://e.txt");
FileChannel fc = fin.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
FileOutputStream fos = new FileOutputStream("F://t.txt");
FileChannel foc = fos.getChannel();
while (true) {
byteBuffer.clear();
int len = fc.read(byteBuffer);
if (len == -1) {
break;
}
byteBuffer.flip();
foc.write(byteBuffer);
byteBuffer.flip();
System.out.println(decode(byteBuffer));
}
foc.close();
fc.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String decode(ByteBuffer sendBuffer) {// ByteBuffer read
// String
Charset cs = Charset.forName("utf-8");
byte[] bs = new byte[sendBuffer.limit()];
sendBuffer.get(bs);
return new String(bs, cs);
// return new String(sendBuffer.array(),cs);
}
}
package nio;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
<strong><span style="font-size:24px;color:#ff0000;">//输入格式化数据</span></strong>
public class Test7 {
<span style="white-space:pre"> </span>public static void main(String[] args) throws Exception {
<span style="white-space:pre"> </span>ByteBuffer bookBuf = ByteBuffer.wrap("java".getBytes("utf-8"));
<span style="white-space:pre"> </span>ByteBuffer autBuf = ByteBuffer.wrap("tianyi".getBytes("utf-8"));
<span style="white-space:pre"> </span>int booklen = bookBuf.limit();
<span style="white-space:pre"> </span>int authlen = autBuf.limit();
<span style="white-space:pre"> </span>System.out.println(booklen + " " + authlen);
<span style="white-space:pre"> </span>ByteBuffer[] bufs = new ByteBuffer[] { bookBuf, autBuf };
<span style="white-space:pre"> </span>File file = new File("F://t1.txt");
<span style="white-space:pre"> </span>if (!file.exists()) {
<span style="white-space:pre"> </span>file.createNewFile();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>// FileOutputStream fos = new FileOutputStream(file);
<span style="white-space:pre"> </span>// FileChannel fc = fos.getChannel();
<span style="white-space:pre"> </span>// fc.write(bufs);
<span style="white-space:pre"> </span>// fos.close();
<span style="white-space:pre"> </span>ByteBuffer b1 = ByteBuffer.allocate(23);
<span style="white-space:pre"> </span>ByteBuffer b2 = ByteBuffer.allocate(9);
<span style="white-space:pre"> </span>bufs = new ByteBuffer[] { b1, b2 };
<span style="white-space:pre"> </span>FileInputStream fis = new FileInputStream(file);
<span style="white-space:pre"> </span>FileChannel fc = fis.getChannel();
<span style="white-space:pre"> </span>fc.read(bufs);
<span style="white-space:pre"> </span>String bookname = new String(bufs[0].array(), "utf-8");
<span style="white-space:pre"> </span>String autname = new String(bufs[1].array(), "utf-8");
<span style="white-space:pre"> </span>System.out.println(bookname + autname);
<span style="white-space:pre"> </span>}
}
package nio;
//传统IO与NIO性能测试对比
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Test8 {
<span style="white-space:pre"> </span>static int numOfInts = 8000000;
<span style="white-space:pre"> </span>public static void main(String[] args) throws Exception {
<span style="white-space:pre"> </span>System.out.println("传统IO");
<span style="white-space:pre"> </span>ioTest1();
<span style="white-space:pre"> </span>System.out.println("NIO");
<span style="white-space:pre"> </span>ioTest2();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public static void ioTest2() throws Exception {
<span style="white-space:pre"> </span>// NIO
<span style="white-space:pre"> </span>long start = System.currentTimeMillis();
<span style="white-space:pre"> </span>FileOutputStream fout = new FileOutputStream(new File("F://ctio.txt"));
<span style="white-space:pre"> </span>FileChannel fc = fout.getChannel();
<span style="white-space:pre"> </span>ByteBuffer byteBuffer = ByteBuffer.allocate(numOfInts * 4);
<span style="white-space:pre"> </span>for (int i = 0; i < numOfInts; i++) {
<span style="white-space:pre"> </span>byteBuffer.put(int2byte(i));
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>long mid = System.currentTimeMillis();
<span style="white-space:pre"> </span>System.out.println((mid - start) );
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>byteBuffer.flip();
<span style="white-space:pre"> </span>fc.write(byteBuffer);
<span style="white-space:pre"> </span> mid = System.currentTimeMillis();
<span style="white-space:pre"> </span>System.out.println((mid - start) );
<span style="white-space:pre"> </span>FileInputStream fin = new FileInputStream(new File("F://ctio.txt"));
<span style="white-space:pre"> </span>FileChannel fic = fin.getChannel();
<span style="white-space:pre"> </span>byteBuffer = ByteBuffer.allocate(numOfInts * 4);
<span style="white-space:pre"> </span>fic.read(byteBuffer);
<span style="white-space:pre"> </span>fic.close();
<span style="white-space:pre"> </span>byteBuffer.flip();
<span style="white-space:pre"> </span>while ((byteBuffer.hasRemaining())) {
<span style="white-space:pre"> </span>byteBuffer.get();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>long end = System.currentTimeMillis();
<span style="white-space:pre"> </span>System.out.println((end - mid) );
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>/** 32位int转byte[] */
<span style="white-space:pre"> </span>public static byte[] int2byte(int res) {
<span style="white-space:pre"> </span>byte[] targets = new byte[4];
<span style="white-space:pre"> </span>targets[0] = (byte) (res & 0xff);// 最低位
<span style="white-space:pre"> </span>targets[1] = (byte) ((res >> 8) & 0xff);// 次低位
<span style="white-space:pre"> </span>targets[2] = (byte) ((res >> 16) & 0xff);// 次高位
<span style="white-space:pre"> </span>targets[3] = (byte) (res >>> 24);// 最高位,无符号右移。
<span style="white-space:pre"> </span>return targets;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>/**
<span style="white-space:pre"> </span> * 将长度为2的byte数组转换为16位int
<span style="white-space:pre"> </span> *
<span style="white-space:pre"> </span> * @param res
<span style="white-space:pre"> </span> * byte[]
<span style="white-space:pre"> </span> * @return int
<span style="white-space:pre"> </span> * */
<span style="white-space:pre"> </span>public static int byte2int(byte b1, byte b2, byte b3, byte b4) {
<span style="white-space:pre"> </span>// res = InversionByte(res);
<span style="white-space:pre"> </span>// 一个byte数据左移24位变成0x??000000,再右移8位变成0x00??0000
<span style="white-space:pre"> </span>int targets = (b1 & 0xff) << 24 | ((b2 & 0xff) << 16)
<span style="white-space:pre"> </span>| ((b3 & 0xff) << 8) | (b4 & 0xff); // | 表示安位或
<span style="white-space:pre"> </span>return targets;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public static void ioTest1() throws Exception {
<span style="white-space:pre"> </span>// 传统IO
<span style="white-space:pre"> </span>long start = System.currentTimeMillis();
<span style="white-space:pre"> </span>DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(
<span style="white-space:pre"> </span>new FileOutputStream(new File("F://ctio.txt"))));
<span style="white-space:pre"> </span>for (int i = 0; i < numOfInts; i++) {
<span style="white-space:pre"> </span>dos.writeInt(i);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if (dos != null) {
<span style="white-space:pre"> </span>dos.close();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>long mid = System.currentTimeMillis();
<span style="white-space:pre"> </span>System.out.println((mid - start) );
<span style="white-space:pre"> </span>DataInputStream dis = new DataInputStream(new BufferedInputStream(
<span style="white-space:pre"> </span>new FileInputStream(new File("F://ctio.txt"))));
<span style="white-space:pre"> </span>for (int i = 0; i < numOfInts; i++) {
<span style="white-space:pre"> </span>dis.readInt();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if (dis != null) {
<span style="white-space:pre"> </span>dis.close();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>long end = System.currentTimeMillis();
<span style="white-space:pre"> </span>System.out.println((end - mid) );
<span style="white-space:pre"> </span>}
}