管道是 2 个线程之间的单向数据连接
一个 source 通道(读取)和一个sink 通道(写入)
打开管道
Pipe pipe = Pipe.open();
写入管道
需要访问 sink 通道
Pipe.SinkChannel sinkChannel = pipe.sink();
读取数据
需要访问 source 通道
Pipe.SourceChannel sourceChannel = pipe.source();
完整代码如下:
public class PipeDemo {
public static void main(String[] args) throws IOException {
//1 获取管道
Pipe pipe = Pipe.open();
//2 获取sink通道
Pipe.SinkChannel sinkChannel = pipe.sink();
//3 创建缓冲区
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
byteBuffer.put("码农研究僧".getBytes());
byteBuffer.flip();
//4 写入数据
sinkChannel.write(byteBuffer);
//5 获取source通道
Pipe.SourceChannel sourceChannel = pipe.source();
//6 读取数据
ByteBuffer byteBuffer2 = ByteBuffer.allocate(1024);
//byteBuffer.flip();
int length = sourceChannel.read(byteBuffer2);
System.out.println(new String(byteBuffer2.array(),0,length));
//7 关闭通道
sourceChannel.close();
sinkChannel.close();
}
}
文件锁是进程级别的,不是线程级别的
具体分为两大类:
**排它锁:**又叫独占锁。对文件加排它锁后,该进程可以对此文件进行读写,该进程独占此文件,其他进程不能读写此文件,直到该进程释放文件锁。
**共享锁:**某个进程对文件加共享锁,其他进程也可以访问此文件,但这些进程都只能读此文件,不能写。线程是安全的。只要还有一个进程持有共享锁,此文件就只能读,不能写
//创建 FileChannel 对象,文件锁只能通过 FileChannel 对象来使用 FileChannel fileChannel=new FileOutputStream("./1.txt").getChannel(); //对文件加锁 FileLock lock=fileChannel.lock(); //对此文件进行一些读写操作。 //....... //释放锁 lock.release();
lock是阻塞,trylock是非阻塞
获取文件锁的方法有如下:
lock()
//对整个文件加锁,默认为排它锁。lock(long position, long size, booean shared)
//自定义加锁方式。tryLock()
//对整个文件加锁,默认为排它锁。tryLock(long position, long size, booean shared)
//自定义加锁方式。FileLock另外两个方法:
boolean isShared()
//此文件锁是否是共享锁boolean isValid()
//此文件锁是否还有效
path,StandardOpenOption.WRITE,StandardOpenOption.APPEND
分别是路径,读写数据,追加数据public class FileLockDemo1 { public static void main(String[] args) throws Exception { String input = "manongyanjiuseng"; System.out.println("input:"+input); ByteBuffer buffer = ByteBuffer.wrap(input.getBytes()); String filePath = "b//1.txt"; Path path = Paths.get(filePath); FileChannel channel = FileChannel.open(path, StandardOpenOption.WRITE,StandardOpenOption.APPEND); channel.position(channel.size()-1); //加锁 FileLock lock = channel.lock(0L,Long.MAX_VALUE,true); System.out.println("是否共享锁:"+lock.isShared()); channel.write(buffer); channel.close(); //读文件 readFile(filePath); } private static void readFile(String filePath) throws Exception { FileReader fileReader = new FileReader(filePath); BufferedReader bufferedReader = new BufferedReader(fileReader); String tr = bufferedReader.readLine(); System.out.println("读取出内容:"); while(tr != null) { System.out.println(" "+tr); tr = bufferedReader.readLine(); } fileReader.close(); bufferedReader.close(); } }
此处包括了path和files等
也是当前的绝对路径
Path path = Paths.get("b://1.txt");
也可以创建相对路径,使用 Paths.get(basePath, relativePath)
方法创建一个相对路径
Path path = Paths.get("b://txt”,“1.txt");
Path.normalize()
String originalPath = "b://projects//..//yygh-project";
Path path1 = Paths.get(originalPath);
System.out.println("path1 = " + path1);
Path path2 = path1.normalize();
System.out.println("path2 = " + path2);
Files.createDirectory()
根据 Path 实例创建一个新目录
Path path = Paths.get("b://1");
try {
Path directory = Files.createDirectory(path);
} catch (IOException e) {
e.printStackTrace();
}
Path path1 = Paths.get("b://1.txt");
Path path2 = Paths.get("b://2.txt");
try {
Path copy = Files.copy(path1, path2, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
Path sourcePath = Paths.get("b://1.txt");
Path destinationPath = Paths.get("b://2.txt");
try {
Files.move(sourcePath, destinationPath,
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
//移动文件失败
e.printStackTrace();
}
Path path = Paths.get("b://1.txt");
try {
Files.delete(path);
} catch (IOException e) {
// 删除文件失败
e.printStackTrace();
}
异步地将数据写入文件
(1)创建了一个 AsynchronousFileChannel
(2)创建一个 ByteBuffer,它被传递给 read()方法作为参数,以及一个 0 的位置。
(3)在调用 read()之后,循环,直到返回的 isDone()方法返回 true。
(4)读取操作完成后,数据读取到 ByteBuffer 中,然后打印到 System.out 中
@Test
public void readAsyncFileChannelFuture() throws Exception {
//1 创建AsynchronousFileChannel
Path path = Paths.get("b://1.txt");
AsynchronousFileChannel fileChannel =
AsynchronousFileChannel.open(path, StandardOpenOption.READ);
//2 创建Buffer
ByteBuffer buffer = ByteBuffer.allocate(1024);
//3 调用channel的read方法得到Future
Future future = fileChannel.read(buffer, 0);
//4 判断是否完成 isDone,返回true
while(!future.isDone());
//5 读取数据到buffer里面
buffer.flip();
// while(buffer.remaining()>0) {
// System.out.println(buffer.get());
// }
byte[] data = new byte[buffer.limit()];
buffer.get(data);
System.out.println(new String(data));
buffer.clear();
}
(1)读取操作完成,将调用 CompletionHandler 的 completed()方法。
(2)对于 completed()方法的参数传递一个整数,它告诉我们读取了多少字节,以及传递给 read()方法的“附件”。“附件”是 read()方法的第三个参数。在本代码中,它是 ByteBuffer,数据也被读取。
(3)如果读取操作失败,则将调用 CompletionHandler 的 failed()方法
@Test
public void readAsyncFileChannelComplate() throws Exception {
//1 创建AsynchronousFileChannel
Path path = Paths.get("b://1.txt");
AsynchronousFileChannel fileChannel =
AsynchronousFileChannel.open(path, StandardOpenOption.READ);
//2 创建Buffer
ByteBuffer buffer = ByteBuffer.allocate(1024);
//3 调用channel的read方法得到Future
fileChannel.read(buffer, 0, buffer, new CompletionHandler() {
@Override
public void completed(Integer result, ByteBuffer attachment) {
System.out.println("result: "+result);
attachment.flip();
byte[] data = new byte[attachment.limit()];
attachment.get(data);
System.out.println(new String(data));
attachment.clear();
}
@Override
public void failed(Throwable exc, ByteBuffer attachment) {
}
});
}
通过 Future 写数据
@Test
public void writeAsyncFileFuture() throws IOException {
//1 创建AsynchronousFileChannel
Path path = Paths.get("b://1.txt");
AsynchronousFileChannel fileChannel =
AsynchronousFileChannel.open(path, StandardOpenOption.WRITE);
//2 创建Buffer
ByteBuffer buffer = ByteBuffer.allocate(1024);
//3 write方法
buffer.put("atguigu ".getBytes());
buffer.flip();
Future future = fileChannel.write(buffer, 0);
while(!future.isDone());
buffer.clear();
System.out.println("write over");
}
通过 CompletionHandler 写数据
@Test
public void writeAsyncFileComplate() throws IOException {
//1 创建AsynchronousFileChannel
Path path = Paths.get("b://1.txt");
AsynchronousFileChannel fileChannel =
AsynchronousFileChannel.open(path, StandardOpenOption.WRITE);
//2 创建Buffer
ByteBuffer buffer = ByteBuffer.allocate(1024);
//3 write方法
buffer.put("atguigujavajava".getBytes());
buffer.flip();
fileChannel.write(buffer, 0, buffer, new CompletionHandler() {
@Override
public void completed(Integer result, ByteBuffer attachment) {
System.out.println("bytes written: " + result);
}
@Override
public void failed(Throwable exc, ByteBuffer attachment) {
}
});
System.out.println("write over");
}