IO

  • StandardCharsets:字符集常量类

获取文件流&&工具类Files

InputStream is = Files.newInputStream(Paths.get(""));
OutputStream os = Files.newOutputStream(Paths.get(""));
byte[] bytes = Files.readAllBytes(Paths.get("")); //读取所有字节

Files.readAllLines(Paths.get(""));
Files.lines(Paths.get(""));

// 追加文件
Files.write(Paths.get(""), "".getBytes(), StandardOpenOption.APPEND);
Files.write(Paths.get(""), Arrays.asList(""), StandardCharsets.UTF_8, StandardOpenOption.APPEND);

// 随机读写文件
RandomAccessFile file = new RandomAccessFile(Paths.get("").toFile(), "rw");

// 内存映射文件
FileChannel channel = FileChannel.open(Paths.get(""), StandardOpenOption.READ, StandardOpenOption.WRITE);
ByteBuffer buffer = channel.map(MapMode.READ_WRITE, 0, channel.size());
buffer.put("hello".getBytes());
//文件锁
channel.lock();
channel.tryLock();

// 删除文件
Files.delete(Paths.get("")); // 如果指定的文件或目录不存在则抛出异常
Files.deleteIfExists(Paths.get(""));

StandardCopyOption

StandardOpenOption

results matching ""

    No results matching ""