NIO--FileLock,Path,Files,AsynchronousFileChannel,Charset

x33g5p2x  于2021-12-10 转载在 其他  
字(12.2k)|赞(0)|评价(0)|浏览(200)

FileLock

文件锁分类

  • 排它锁:又叫独占锁。对文件加排它锁后,该进程可以对此文件进行读写,该进程独占此文件,其他进程不能读写此文件,直到该进程释放文件锁。
  • 共享锁:某个进程对文件加共享锁,其他进程也可以访问此文件,但这些进程都只能读此文件,不能写。线程是安全的。只要还有一个进程持有共享锁,此文件就只能 读,不能写。

使用示例

//创建 FileChannel 对象,文件锁只能通过 FileChannel 对象来使用
FileChannel fileChannel=new FileOutputStream("./1.txt").getChannel();
//对文件加锁
FileLock lock=fileChannel.lock();
//对此文件进行一些读写操作。
//...
//释放锁
lock.release();

文件锁要通过 FileChannel 对象使用

获取文件锁方法

有 4 种获取文件锁的方法:

lock() //对整个文件加锁,默认为排它锁
lock(long position, long size, booean shared) //自定义加锁方式。前 2 个参数
//指定要加锁的部分(可以只对此文件的部分内容加锁),第三个参数值指定是否是共
//享锁
tryLock() //对整个文件加锁,默认为排它锁。
tryLock(long position, long size, booean shared) //自定义加锁方式。

如果指定为共享锁,则其它进程可读此文件,所有进程均不能写此文件,如果某进程试图对此文件进行写操作,会抛出异常。

lock 与 tryLock 的区别:

lock 是阻塞式的,如果未获取到文件锁,会一直阻塞当前线程,直到获取文件锁

tryLock 和 lock 的作用相同,只不过 tryLock 是非阻塞式的,tryLock 是尝试获取文件锁,获取成功就返回锁对象,否则返回 null,不会阻塞当前线程。

FileLock 两个方法:

boolean isShared() //此文件锁是否是共享锁
boolean isValid() //此文件锁是否还有效

在某些 OS 上,对某个文件加锁后,不能对此文件使用通道映射。

example

public class Demo1 {
 public static void main(String[] args) throws IOException {
 String input = "dhy";
 System.out.println("输入 :" + input);
 ByteBuffer buf = ByteBuffer.wrap(input.getBytes());
 String fp = "D:\\dhy\\01.txt";
 Path pt = Paths.get(fp);
 FileChannel channel = FileChannel.open(pt, 
StandardOpenOption.WRITE,StandardOpenOption.APPEND);
 channel.position(channel.size() - 1); // position of a cursor at the end of file
 // 获得锁方法一:lock(),阻塞方法,当文件锁不可用时,当前进程会被挂起
 //lock = channel.lock();// 无参 lock()为独占锁
 // lock = channel.lock(0L, Long.MAX_VALUE, true);//有参 lock()为共享锁,有写操作会报异常
 // 获得锁方法二:trylock(),非阻塞的方法,当文件锁不可用时,tryLock()会得到 null 值
 FileLock lock = channel.tryLock(0,Long.MAX_VALUE,false);
 System.out.println("共享锁 shared: " + lock.isShared());
 channel.write(buf);
 channel.close(); // Releases the Lock
 System.out.println("写操作完成.");
 //读取数据
 readPrint(fp);
 }
 public static void readPrint(String path) throws IOException {
 FileReader filereader = new FileReader(path);
 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

创建 Path 实例

使用 java.nio.file.Path 实例必须创建一个 Path 实例。可以使用 Paths 类(java.nio.file.Paths)中的静态方法 Paths.get()来创建路径实例。

import java.nio.file.Path;
import java.nio.file.Paths;
public class PathDemo {
 public static void main(String[] args) {
 Path path = Paths.get("d:\\dhy\\001.txt");
 } }

上述代码,可以理解为,Paths.get()方法是 Path 实例的工厂方法。

创建绝对路径

(1)创建绝对路径,通过调用 Paths.get()方法,给定绝对路径文件作为参数来完成。

Path path = Paths.get("d:\\dhy\\001.txt");

上述代码中,绝对路径是 d:\dhy\001.txt。在 Java 字符串中, \是一个转义字符,需要编写\,告诉 Java 编译器在字符串中写入一个\字符。

(2)如果在 Linux、MacOS 等操作字体上,上面的绝对路径可能如下:

Path path = Paths.get("/home/jakobjenkov/myfile.txt");

绝对路径现在为/home/jakobjenkov/myfile.txt.

(3)如果在 Windows 机器上使用了从/开始的路径,那么路径将被解释为相对于当前驱动器。

创建相对路径

Java NIO Path 类也可以用于处理相对路径。您可以使用 Paths.get(basePath, relativePath)方法创建一个相对路径。

//代码 1
Path projects = Paths.get("d:\\dhy", "projects");
//代码 2
Path file = Paths.get("d:\\dhy", "projects\\002.txt");

代码 1 创建了一个 Java Path 的实例,指向路径(目录):d:\dhy\projects

代码 2 创建了一个 Path 的实例,指向路径(文件):d:\dhy\projects\002.txt

Path.normalize()

Path 接口的 normalize()方法可以使路径标准化。标准化意味着它将移除所有在路径字符串的中间的.和…代码,并解析路径字符串所引用的路径。

String originalPath =
 "d:\\dhy\\projects\\..\\yygh-project";
Path path1 = Paths.get(originalPath);
System.out.println("path1 = " + path1);
Path path2 = path1.normalize();
System.out.println("path2 = " + path2);

输出结果:标准化的路径不包含 projects…部分

path1 =d:\dhy\projects\..\yygh-project
path2 =d:\dhy\yygh-project

Files

Java NIO Files 类(java.nio.file.Files)提供了几种操作文件系统中的文件的方法。以下内容介绍 Java NIO Files 最常用的一些方法。

java.nio.file.Files 类与java.nio.file.Path 实例一起工作,因此在学习 Files 类之前,需要先了解 Path 类

Files.createDirectory()

Files.createDirectory()方法,用于根据 Path 实例创建一个新目录

Path path = Paths.get("d:\\sgg");
try {
 Path newDir = Files.createDirectory(path);
} catch(FileAlreadyExistsException e){
 // 目录已经存在
} catch (IOException e) {
 // 其他发生的异常
 e.printStackTrace();
}

第一行创建表示要创建的目录的 Path 实例。在 try-catch 块中,用路径作为参数调用Files.createDirectory()方法。如果创建目录成功,将返回一个 Path 实例,该实例指向新创建的路径。

如果该目录已经存在,则是抛出一个 java.nio.file.FileAlreadyExistsException。如果出现其他错误,可能会抛出 IOException。例如,如果想要的新目录的父目录不存在,则可能会抛出 IOException

Files.copy()

(1)Files.copy()方法从一个路径拷贝一个文件到另外一个目录

Path sourcePath = Paths.get("d:\\dhyy\\01.txt");
Path destinationPath = Paths.get("d:\\dhyy\\002.txt");
try {
 Files.copy(sourcePath, destinationPath);
} catch(FileAlreadyExistsException e) {
 // 目录已经存在
} catch (IOException e) {
 // 其他发生的异常
 e.printStackTrace();
}

首先,该示例创建两个 Path 实例。然后,这个例子调用 Files.copy(),将两个 Path实例作为参数传递。这可以让源路径引用的文件被复制到目标路径引用的文件中。

如果目标文件已经存在,则抛出一个 java.nio.file.FileAlreadyExistsException 异常。

如果有其他错误,则会抛出一个 IOException。例如,如果将该文件复制到不存在的目录,则会抛出 IOException。

(2)覆盖已存在的文件

Files.copy()方法的第三个参数。如果目标文件已经存在,这个参数指示 copy()方法覆盖现有的文件。

Files.copy(sourcePath, destinationPath, 
 StandardCopyOption.REPLACE_EXISTING);

Files.move()

Files.move()用于将文件从一个路径移动到另一个路径。移动文件与重命名相同,但是移动文件既可以移动到不同的目录,也可以在相同的操作中更改它的名称。

Path sourcePath = Paths.get("d:\\dhy\\01.txt");
Path destinationPath = Paths.get("d:\\dhy\\001.txt");
try {
 Files.move(sourcePath, destinationPath,
 StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
 //移动文件失败
 e.printStackTrace();
}

Files.move()的第三个参数。这个参数告诉 Files.move()方法来覆盖目标路径上的任何现有文件。

Files.delete()

Files.delete()方法可以删除一个文件或者目录。

Path path = Paths.get("d:\\dhy\\001.txt");
try {
 Files.delete(path);
} catch (IOException e) {
 // 删除文件失败
 e.printStackTrace();
}

创建指向要删除的文件的 Path。然后调用 Files.delete()方法。如果 Files.delete()不能删除文件(例如,文件或目录不存在),会抛出一个 IOException。

Files.walkFileTree()

(1)Files.walkFileTree()方法包含递归遍历目录树功能,将 Path 实例和 FileVisitor作为参数。Path 实例指向要遍历的目录,FileVisitor 在遍历期间被调用。

(2)FileVisitor 是一个接口,必须自己实现 FileVisitor 接口,并将实现的实例传递给walkFileTree()方法。在目录遍历过程中,您的 FileVisitor 实现的每个方法都将被调用。如果不需要实现所有这些方法,那么可以扩展 SimpleFileVisitor 类,它包含FileVisitor 接口中所有方法的默认实现

(3)FileVisitor 接口的方法中,每个都返回一个 FileVisitResult 枚举实例。FileVisitResult 枚举包含以下四个选项:

CONTINUE 继续
TERMINATE 终止
SKIP_SIBLING 跳过同级
SKIP_SUBTREE 跳过子级

(4)查找一个名为 001.txt 的文件示例:

Path rootPath = Paths.get("d:\\dhy");
String fileToFind = File.separator + "001.txt";
try {
 Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {
 @Override
 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws 
IOException {
 String fileString = file.toAbsolutePath().toString();
 //System.out.println("pathString = " + fileString);
  if(fileString.endsWith(fileToFind)){
 System.out.println("file found at path: " + file.toAbsolutePath());
 return FileVisitResult.TERMINATE;
 }
 return FileVisitResult.CONTINUE;
 }
 });
} catch(IOException e){
 e.printStackTrace();
}

(5)java.nio.file.Files 类包含许多其他的函数,有关这些方法的更多信息,请查看java.nio.file.Files 类的 JavaDoc

AsynchronousFileChannel

Java NIO中的FileChannel是一个连接到文件的通道。可以通过文件通道读写文件。FileChannel无法设置为非阻塞模式,他总是运行在阻塞模式下。在Java 7中,AsynchronousFileChannel被添加到Java NIO。AsynchronousFileChannel使读取数据,并异步地将数据写入文件成为可能。

创建 AsynchronousFileChannel

通过静态方法 open()创建

Path path = Paths.get("d:\\dhy\\01.txt");
try {
 AsynchronousFileChannel fileChannel =
 AsynchronousFileChannel.open(path, StandardOpenOption.READ);
} catch (IOException e) {
 e.printStackTrace();
}

open()方法的第一个参数指向与 AsynchronousFileChannel 相关联文件的 Path 实例。

第二个参数是一个或多个打开选项,它告诉 AsynchronousFileChannel 在文件上执行什么操作。在本例中,我们使用了 StandardOpenOption.READ 选项,表示该文件将被打开阅读。

通过 Future 读取数据

可以通过两种方式从 AsynchronousFileChannel 读取数据。第一种方式是调用返回Future 的 read()方法

Path path = Paths.get("d:\\dhy\\001.txt");
AsynchronousFileChannel fileChannel = null;
try {
 fileChannel = AsynchronousFileChannel.open(path, 
StandardOpenOption.READ);
} catch (IOException e) {
 e.printStackTrace();
}
ByteBuffer buffer = ByteBuffer.allocate(1024);
long position = 0;
Future<Integer> operation = fileChannel.read(buffer, position);
while(!operation.isDone());
buffer.flip();
byte[] data = new byte[buffer.limit()];
buffer.get(data);
System.out.println(new String(data));
buffer.clear();

上述代码:
(1)创建了一个 AsynchronousFileChannel,
(2)创建一个 ByteBuffer,它被传递给 read()方法作为参数,以及一个 0 的位置。
(3)在调用 read()之后,循环,直到返回的 isDone()方法返回 true。
(4)读取操作完成后,数据读取到 ByteBuffer 中,然后打印到 System.out 中。

注意:

read()方法的这个版本将ByteBuffer作为第一个参数。从AsynchronousFileChannel读取的数据被读入这个ByteBuffer。第二个参数是文件中的字节位置,以便开始读取。

read()方法会立即返回,即使读操作还没有完成。通过调用read()方法返回的Future实例的isDone()方法,您可以检查读取操作是否完成。

上面的程序首先创建了一个 AsynchronousFileChannel 对象,然后调用它的read()方法返回一个Future。其中read()方法需要两个参数,一个是ByteBuffer,另一个是读取文件的开始位置。然后通过循环调用isDone() 方法检测读取过程是否完成,完成后 isDone()方法将返回true。尽管这样让cpu空转了一会,但是我们还是应该等读取操作完成后再进行后续的步骤。

一旦读取完成,数据被存储到ByteBuffer,然后将数据转化为字符串既而输出。

通过 CompletionHandler 读取数据

第二种读取数据的方式是调用AsynchronousFileChannel 的另一个重载 read() 方法,改方法需要一个CompletionHandler 作为参数

Path path = Paths.get("d:\\dhy\\001.txt");
AsynchronousFileChannel fileChannel = null;
try {
 fileChannel = AsynchronousFileChannel.open(path, 
StandardOpenOption.READ);
} catch (IOException e) {
 e.printStackTrace();
}
ByteBuffer buffer = ByteBuffer.allocate(1024);
long position = 0;
fileChannel.read(buffer, position, buffer, new CompletionHandler<Integer, ByteBuffer>() {
 @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) {
 }
});

一旦读取操作完成,CompletionHandler的 complete() 方法将会被调用。它的第一个参数是个 Integer类型,表示读取的字节数。第二个参数 attachment 是 ByteBuffer 类型的,用来存储读取的数据。它其实就是由 read() 方法的第三个参数。当前示例中,我们选用 ByteBuffer 来存储数据,其实我们也可以选用其他的类型。读取失败的时候,CompletionHandler的 failed()方法会被调用。

通过 Future 写数据

Path path = Paths.get("d:\\dhy\\001.txt");
AsynchronousFileChannel fileChannel = null;
try {
 fileChannel = AsynchronousFileChannel.open(path, 
StandardOpenOption.WRITE);
} catch (IOException e) {
 e.printStackTrace();
}
ByteBuffer buffer = ByteBuffer.allocate(1024);
long position = 0;
buffer.put("huyou data".getBytes());
buffer.flip();
Future<Integer> operation = fileChannel.write(buffer, position);
buffer.clear();
while(!operation.isDone());
System.out.println("Write over");

首先,AsynchronousFileChannel 以写模式打开。然后创建一个 ByteBuffer,并将一些数据写入其中。然后,ByteBuffer 中的数据被写入到文件中。最后,示例检查返回的 Future,以查看写操作完成时的情况。

注意,文件必须已经存在。如果该文件不存在,那么 write()方法将抛出一个java.nio.file.NoSuchFileException

通过 CompletionHandler 写数据

Path path = Paths.get("d:\\dhy\\001.txt");
if(!Files.exists(path)){
 try {
 Files.createFile(path);
 } catch (IOException e) {
 e.printStackTrace();
 } }
AsynchronousFileChannel fileChannel = null;
try {
 fileChannel = AsynchronousFileChannel.open(path, 
StandardOpenOption.WRITE);
} catch (IOException e) {
 e.printStackTrace();
}
ByteBuffer buffer = ByteBuffer.allocate(1024);
long position = 0;
buffer.put("hyyou data".getBytes());
buffer.flip();
fileChannel.write(buffer, position, buffer, new CompletionHandler<Integer, ByteBuffer>() {
 @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 failed");
 exc.printStackTrace();
 }
});

当写操作完成时,将会调用 CompletionHandler 的 completed()方法。如果写失败,则会调用 failed()方法。

字符集(Charset)

java 中使用 Charset 来表示字符集编码对象

Charset 常用静态方法

public static Charset forName(String charsetName)//通过编码类型获得 Charset 对 象
public static SortedMap<String,Charset> availableCharsets()//获得系统支持的所有编码方式
public static Charset defaultCharset()//获得虚拟机默认的编码方式
public static boolean isSupported(String charsetName)//判断是否支持该编码类型

Charset 常用普通方法

public final String name()//获得 Charset 对象的编码类型(String)
public abstract CharsetEncoder newEncoder()//获得编码器对象
public abstract CharsetDecoder newDecoder()//获得解码器对象

代码示例:

@Test
public void charSetEncoderAndDecoder() throws 
CharacterCodingException {
 Charset charset=Charset.forName("UTF-8");
 //1.获取编码器
 CharsetEncoder charsetEncoder=charset.newEncoder();
 //2.获取解码器
 CharsetDecoder charsetDecoder=charset.newDecoder();
 //3.获取需要解码编码的数据
 CharBuffer charBuffer=CharBuffer.allocate(1024);
 charBuffer.put("字符集编码解码");
 charBuffer.flip();
 //4.编码
 ByteBuffer byteBuffer=charsetEncoder.encode(charBuffer);
 System.out.println("编码后:");
 for (int i=0;i<byteBuffer.limit();i++) {
 System.out.println(byteBuffer.get());
 }
 //5.解码
 byteBuffer.flip();
 CharBuffer charBuffer1=charsetDecoder.decode(byteBuffer);
 System.out.println("解码后:");
 System.out.println(charBuffer1.toString());
  System.out.println("指定其他格式解码:");
 Charset charset1=Charset.forName("GBK");
 byteBuffer.flip();
 CharBuffer charBuffer2 =charset1.decode(byteBuffer);
 System.out.println(charBuffer2.toString());
 //6.获取 Charset 所支持的字符编码
 Map<String ,Charset> map= Charset.availableCharsets();
 Set<Map.Entry<String,Charset>> set=map.entrySet();
 for (Map.Entry<String,Charset> entry: set) {
 System.out.println(entry.getKey()+"="+entry.getValue().toString());
 } }

相关文章