public static void fileOperationInAnyLocation(String filePath)
{
Path path = Paths.get(filePath);
// To check file exist.
if (!path.toFile().exists())
throw new IllegalArgumentException("The file path is not exist.");
ByteBuffer buffer = ByteBuffer.allocate(1024);
try
{
FileChannel channel = FileChannel.open(path, StandardOpenOption.READ, StandardOpenOption.WRITE);
channel.read(buffer, channel.size() - 1000);
System.out.println(new String(buffer.array()));
ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
writeBuffer.put("111111111111".getBytes());
channel.write(writeBuffer, 0);
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Java 文件任意位置读写操作(首尾读写都可以)
最新推荐文章于 2023-09-14 11:04:01 发布