java io nio pio_JAVA NIO

一、使用NIO复制文件

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

package javanio;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.nio.ByteBuffer;

import java.nio.channels.FileChannel;

import java.util.Scanner;

public class CopyFile {

public static void main(String[] args) throws IOException {

CopyFile cf = new CopyFile();

cf.startCopy();

}

public void startCopy() throws IOException {

/* 默认目标文件路径, "." 表示在当前工程目录下 */

String des = ".\\src\\javanio\\Test.txt";

System.out.println("请输入源文件路径:");

Scanner sc = new Scanner(System.in);

String src = sc.next();

sc.close();

File srcFile = new File(src);

File desFile = new File(des);

if (!srcFile.exists()) {

throw new RuntimeException("源文件不存在!");

}

if (!srcFile.canRead()) {

throw new RuntimeException("源文件不可读!");

}

if (!srcFile.exists()) {

throw new RuntimeException("源文件不存在!");

}

if (!srcFile.canRead()) {

throw new RuntimeException("源文件不可读!");

}

/* 获取源文件和目标文件的输入输出流 */

FileInputStream fis = new FileInputStream(srcFile);

FileOutputStream fos = new FileOutputStream(desFile);

/* 获取输入输出通道 */

FileChannel fisChannel = fis.getChannel();

FileChannel fosChannel = fos.getChannel();

/* 创建缓冲区 */

ByteBuffer buffer = ByteBuffer.allocate(1024);

while (true) {

// clear方法重设缓冲区,使它可以接受读入的数据

buffer.clear();

// 从输入通道中将数据读到缓冲区

int r = fisChannel.read(buffer);

// read方法返回读取的字节数,可能为零,如果该通道已到达流的末尾,则返回-1

if (r == -1) {

break;

}

// flip方法让缓冲区可以将新读入的数据写入另一个通道

buffer.flip();

// 从输出通道中将数据写入缓冲区

fosChannel.write(buffer);

}

if (desFile.exists()) {

System.out.println("复制成功!");

}

fis.close();

fos.close();

}

}

使用NIO复制文件

二、IO与NIO比较

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

package javanio;

import java.io.File;

import java.io.FileInputStream;

import java.nio.ByteBuffer;

import java.nio.channels.FileChannel;

public class CompIO {

private static String PATH = ".\\src\\javanio\\Test.txt";

public static void main(String[] args) throws Exception {

useIO();

useNIO();

}

private static void useIO() throws Exception {

File file = new File(PATH);

FileInputStream in = new FileInputStream(file);

byte[] b = new byte[1024];

in.read(b);

in.close();

System.out.println(new String(b, "UTF-8"));

}

private static void useNIO() throws Exception {

File file = new File(PATH);

FileInputStream in = new FileInputStream(file);

FileChannel channel = in.getChannel();

ByteBuffer buffer = ByteBuffer.allocate(1024);

channel.read(buffer);

byte[] b = buffer.array();

in.close();

System.out.println(new String(b, "UTF-8"));

}

}

NIO于IO比较

三、NIO

a、区别

面向流的I/O系统一次一个字节处理数据。一个输入流产生一个字节数据,一个输出流消费一个字节数据。

NIO使用块I/O的处理方式。每一个操作都在一步中产生或者消费一个数据块。按块处理数据比按字节处理数据要快。

b、通道

Channel是对原I/O包中的流模拟。到任何目的地或来自任何地方的所有数据都必须通过一个Channel对象。

c、缓冲区

Buffer实质是一个容器对象。所有数据的处理都在缓冲区中。发送给一个通道的所有对象都必须先发到缓冲区中。同样,从通道中读取的任何数据都要读到缓冲区中。

缓冲区类型:ByteBuffer、CharBuffer、IntBuffer、FloatBuffer等等。

d、读取

1.从FileInputStream获取Channel。

2.创建Buffer。

3.将数据从Channel中读到Buffer中。

e、写入

1.从FileOutputStream中获取一个通道。

2.创建缓冲区,并在其中放入数据。

3.写入缓冲区中。

来源:https://www.cnblogs.com/wscy/p/4753238.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值