目的:数据传输
传统IO:面向字节的流动,流动是单向的
新IO:面向缓冲区,通道
Buffer:缓冲区底层是数组,用来存储数据,针对基本类型,都提供相应的缓冲区
例子:
以ByteBuffe为例:字节缓冲区,
主要属性:
capacity:容量,一旦指定不能更改
Limit:界限,从limit往后的数据不可读写
Position:位置,文件指针,从position开始读取数据
主要方法:
ByteBuffe. allocate():分配缓冲区
ByteBuffe.put():放入数据
ByteBuffe.flip():IQ切换读取模式
ByteBuffe.get():读取数据
ByteBuffe.rewind():重复读取,使指针回到首位
ByteBuffe.clear():清空缓冲区,并不是清除数据,而是把指针设置到最初使状态
ByteBuffe.mark():标记当前position位置
ByteBuffe.reset():返回到上次标记的位置
ByteBuffe.hasRemaining():判断是否还有可读数据
ByteBuffe.remaining():返回还有多少可读数据
两种缓冲区:
非之间缓冲区:将缓冲区建立在jvm的内存中ByteBuffe. allocate()
直接缓冲区:将缓冲区建立在物理内存中ByteBuffe. allocateDirect(),比非直接缓冲区效率高,但是耗费资源
方法一:
使用非直接缓冲区,通过FileInputStream,FileOutputStream,RandomAccessFile中的
getChannel()方法,获取通道
方法二:
FileChannel.Open(Path,StandardOpenOption.READ)
Path =Paths.get(“路径”);
FileChannel.Open(Path,StandardOpenOption. WRITE, StandardOpenOption. CREATE/CREATE_NEW)
StandardOpenOption. CREATE_NEW:文件不存在就创建,存在就报错
StandardOpenOption. CREATE:文件不存在就创建,存在就覆盖
FileChannel:针对本地文件传输
方法三:
Files.newByteChannel(Paths.get("file.txt"), StandardOpenOption.READ);
FileChannel outChannel = (FileChannel)Files.newByteChannel(Paths.get("file2.txt"), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
static long copy(InputStream in, Path target, CopyOption… options)
将所有字节从输入流复制到文件。
static long copy(Path source, OutputStream out)
将从文件到输出流的所有字节复制到输出流中。
static Path copy(Path source, Path target, CopyOption… options) 将一个文件复制到目标文件。
Files 常用方法:
Path copy(Path src, Path dest, CopyOption … how) : 文件的复制
Path createDirectory(Path path, FileAttribute> … attr) : 创建一个目录
Path createFile(Path path, FileAttribute> … arr) : 创建一个文件
void delete(Path path) : 删除一个文件
Path move(Path src, Path dest, CopyOption…how) : 将 src 移动到 dest 位置
long size(Path path) : 返回 path 指定文件的大小
static Path write(Path path, Iterable extends CharSequence> lines, OpenOption... options) 可以将List集合中的数据写到文件中
Files 常用方法:用于判断
boolean exists(Path path, LinkOption … opts) : 判断文件是否存在
boolean isDirectory(Path path, LinkOption … opts) : 判断是否是目录
boolean isExecutable(Path path) : 判断是否是可执行文件
boolean isHidden(Path path) : 判断是否是隐藏文件
boolean isReadable(Path path) : 判断文件是否可读
boolean isWritable(Path path) : 判断文件是否可写
boolean notExists(Path path, LinkOption … opts) : 判断文件是否不存在
public static A readAttributes(Path path,Class type,LinkOption…
options) : 获取与 path 指定的文件相关联的属性。
Files 常用方法:用于操作内容
SeekableByteChannel newByteChannel(Path path, OpenOption…how) : 获取与指定文件的连接,how 指定打开方式。
DirectoryStream newDirectoryStream(Path path) : 打开 path 指定的目录
InputStream newInputStream(Path path, OpenOption…how): 获取 InputStream 对象
OutputStream newOutputStream(Path path, OpenOption…how) : 获取 OutputStream 对象
boolean endsWith(String path) : 判断是否以 path 路径结束
boolean startsWith(String path) : 判断是否以 path 路径开始
boolean isAbsolute() : 判断是否是绝对路径
Path getFileName() : 返回与调用 Path 对象关联的文件名
Path getName(int idx) : 返回的指定索引位置 idx 的路径名称
int getNameCount() : 返回 Path 根目录后面元素的数量
Path getParent() :返回 Path 对象包含整个路径,不包含 Path 对象指定的文件路径
Path getRoot() :返回调用 Path 对象的根路径
Path resolve(Path p) : 将相对路径解析为绝对路径
Path toAbsolutePath() : 作为绝对路径返回调用 Path 对象
String toString() : 返回调用 Path 对象的字符串表示形式
方法一:
FileOutputStream out = new FileOutputStream("file.mp3");
FileInputStream in = new FileInputStream("合并.mp3");
FileChannel inChannel = in.getChannel();
FileChannel outChannel = out.getChannel();
ByteBuffer byteBuffer =ByteBuffer.allocate(1024*1024);//创建缓冲区
// ByteBuffer[] byteBuffers = new ByteBuffer[1024 * 1024];
while((inChannel.read(byteBuffer))!=-1){
byteBuffer.flip();
outChannel.write(byteBuffer);
byteBuffer.clear();
}
inChannel.close();;
outChannel.close();
in.close();
out.close();
方法二:
FileChannel inopen = FileChannel.open(Paths.get("5.mp3"), StandardOpenOption.READ);
FileChannel outopen = FileChannel.open(Paths.get("file1.mp3"), StandardOpenOption.WRITE, StandardOpenOption.CREATE);
ByteBuffer allocate = ByteBuffer.allocate(1024 * 1024);
while(inopen.read(allocate)!=-1){
allocate.flip();
outopen.write(allocate);
allocate.clear();
}
inopen.close();
outopen.close();
方法三:(针对于通道复制文件)
FileChannel open = FileChannel.open(Paths.get("合并.mp3"), StandardOpenOption.READ);
FileChannel open1 = FileChannel.open(Paths.get("file3.mp3"), StandardOpenOption.WRITE, StandardOpenOption.CREATE);
open.transferTo(0,open.size(),open1);
open1.transferFrom(open,0,open.size());
FileChannel open = FileChannel.open(Paths.get("合并.mp3"), StandardOpenOption.READ);
FileChannel open1 = FileChannel.open(Paths.get("file4.mp3"), StandardOpenOption.WRITE, StandardOpenOption.READ,StandardOpenOption.CREATE);
MappedByteBuffer map = open.map(FileChannel.MapMode.READ_ONLY, 0, open.size());
MappedByteBuffer map1 = open1.map(FileChannel.MapMode.READ_WRITE, 0, open.size());
byte[] bytes = new byte[map.limit()];
map.get(bytes);
map1.put(bytes);
open.close();
open1.close();
Files.copy(Paths.get("5.mp3"),Paths.get("file5.mp3"), StandardCopyOption.REPLACE_EXISTING);