Основной класс реализации канала
import java.nio.channels.DatagramChannel;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
способ канала Acquisition
- JAVA обеспечивает поддержку для прохода метода GetChannel класса ()
локального ввода - вывода:
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.RandomAccessFile;
Сеть IO:
import java.net.ServerSocket; import java.net.Socket; import java.nio.channels.DatagramChannel;
- jdk1.7 после того, как NIO.2 обеспечивает статический метод открыт для каждого канала ()
- Файлы в инструменте jdk1.7 NIO.w или как newByteChannel ()
Скопировать файлы закончили использовать канал: тематический
- Непрямой буферы:
import org.junit.jupiter.api.Test; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class ChannelTest { @Test public void channel() throws IOException { FileInputStream fis=new FileInputStream("文件1的路径"); FileOutputStream fos=new FileOutputStream("文件2的路径"); //1.获取通道 FileChannel inChannel=fis.getChannel(); FileChannel outChannel=fos.getChannel(); //2.分配指定大小的缓冲区 ByteBuffer buffer=ByteBuffer.allocate(1024); //3.将通道中的数据存入缓冲区中 while (inChannel.read(buffer)!=-1){ buffer.flip();//切换读取数据的模式 outChannel.write(buffer);//写入数据 buffer.clear();//清空缓冲去 } //关闭流和通道 inChannel.close(); outChannel.close(); fis.close(); fos.close(); } }
- Прямые буферы
import org.junit.jupiter.api.Test; import java.io.IOException; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; public class ChannelTest { @Test public void channel2() throws IOException { FileChannel inchannel=FileChannel.open(Paths.get("文件1的路径"), StandardOpenOption.READ); //CREATE_NEW 如果存在则报错,如果不存在则报错。READ只读模式。WRITE存在则覆盖,不存在则创建 FileChannel outchannel=FileChannel.open(Paths.get("文件2的路径"),StandardOpenOption.WRITE,StandardOpenOption.CREATE_NEW); //内存映射文件 MappedByteBuffer inMappedBuf=inchannel.map(FileChannel.MapMode.READ_ONLY,0,inchannel.size()); MappedByteBuffer outMappedBuf=inchannel.map(FileChannel.MapMode.READ_WRITE,0,inchannel.size()); //对缓冲区进行数据的读写操作 byte[] dst=new byte[inMappedBuf.limit()]; inMappedBuf.get(dst); outMappedBuf.put(dst); inchannel.close(); outchannel.close(); } }
Самый простой способ: о
переходе транспорт
(проблемы между разницей между ними состоит только вперед и назад) передачи transferFromimport org.junit.jupiter.api.Test; import java.io.IOException; import java.nio.channels.FileChannel; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; public class ChannelTest { @Test public void channel3() throws IOException { //读取文件 FileChannel inchannel=FileChannel.open(Paths.get("文件1的位置"),StandardOpenOption.READ); //目标文件的位置 FileChannel outchannel=FileChannel.open(Paths.get("文件2的位置"),StandardOpenOption.CREATE_NEW); inchannel.transferTo(0,inchannel.size(),outchannel); inchannel.close(); outchannel.close(); } }