import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import org.junit.Test;
public class NioMain {
@Test
public void bioread(){
InputStream in = null;
try{
in = new BufferedInputStream(new FileInputStream("src/nomal_io.txt"));
InputStreamReader inputStreamReader = new InputStreamReader(in,"UTF-8");
BufferedReader br = new BufferedReader(inputStreamReader);
String data = null;
while((data = br.readLine())!=null){
System.out.println(data);
}
// byte [] buf = new byte[1024];
// int bytesRead = in.read(buf);
// while(bytesRead != -1){
// for(int i=0;i<bytesRead;i++){
// System.out.print((char)buf[i]);
// }
// bytesRead = in.read(buf);
// }
}catch (IOException e){
e.printStackTrace();
}finally{
try{
if(in != null){
in.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
@Test
public void readByte(){
//数据总是从通道读取到缓冲区中,或者从缓冲区写入到通道中
RandomAccessFile aFile = null;
try{
aFile = new RandomAccessFile("src/nio.txt","rw");
FileChannel fileChannel = aFile.getChannel();
ByteBuffer buf = ByteBuffer.allocate(1024);
int bytesRead = fileChannel.read(buf);
while(bytesRead != -1){
buf.flip();
while(buf.hasRemaining()){
System.out.print((char)buf.get());
}
buf.compact();
bytesRead = fileChannel.read(buf);
}
}catch (IOException e){
e.printStackTrace();
}
finally{
try{
if(aFile != null){
aFile.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
/**
*
*/
@Test
public void readStr(){
RandomAccessFile raf =null;
try {
Charset charset = Charset.forName("UTF-8");
CharsetDecoder decoder = charset.newDecoder();
raf = new RandomAccessFile("src/nio.txt", "rw");
FileChannel fc = raf.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(512);
CharBuffer cb = CharBuffer.allocate(512);
int count = fc.read(buffer);
while (count != -1) {
buffer.flip();
decoder.decode(buffer, cb, false);
cb.flip();
while (cb.hasRemaining()) {
System.out.print(cb.get());
}
buffer.clear();
cb.clear();
count = fc.read(buffer);
}
} catch (Exception e) {
e.printStackTrace();
}
finally{
try{
if(raf != null){
raf.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
/**
* nio 拷贝文件
*/
@Test
public void copyfile(){
RandomAccessFile fromFile = null;
RandomAccessFile toFile = null;
try{
fromFile = new RandomAccessFile("src/fromFile.txt","rw");
FileChannel fromChannel = fromFile.getChannel();
toFile = new RandomAccessFile("src/toFile.txt","rw");
FileChannel toChannel = toFile.getChannel();
fromChannel.transferTo(0,fromChannel.size(),toChannel);
}catch (IOException e){
e.printStackTrace();
}
finally{
try{
if(fromFile != null){
fromFile.close();
}
if(toFile != null){
toFile.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}
转载于:https://my.oschina.net/v512345/blog/1530080