文件移动/重命名时-什么';java RAF和文件的区别是什么
我只想在java/linux上下文中讨论这一点文件移动/重命名时-什么';java RAF和文件的区别是什么,java,java-io,inode,Java,Java Io,Inode,我只想在java/linux上下文中讨论这一点 RandomAccessFile rand = new RandomAccessFile("test.log", "r"); VS 创建之后,我们开始将文件读到底 在java.io.File的情况下,如果在读取文件之前修改或删除物理文件,则在读取文件时将抛出IOException public void readIOFile() throws IOException, InterruptedException { File file =
RandomAccessFile rand = new RandomAccessFile("test.log", "r");
VS
创建之后,我们开始将文件读到底
public void readIOFile() throws IOException, InterruptedException {
File file = new File("/tmp/test.log");
System.out.print("file created"); // convert byte into char
Thread.sleep(5000);
while (true) {
char[] buffer = new char[1024];
FileReader fr = new FileReader(file);
fr.read(buffer);
System.out.println(buffer);
}
}
public void readRAF() throws IOException, InterruptedException {
File file = new File("/tmp/test.log");
RandomAccessFile rand = new RandomAccessFile(file, "rw");
System.out.println("file created"); // convert byte into char
while (true) {
System.out.println(file.lastModified());
System.out.println(file.length());
Thread.sleep(5000);
System.out.println("finish sleeping");
int i = (int) rand.length();
rand.seek(0); // Seek to start point of file
for (int ct = 0; ct < i; ct++) {
byte b = rand.readByte(); // read byte from the file
System.out.print((char) b); // convert byte into char
}
}
public void readRAF()引发IOException、InterruptedException{
File File=新文件(“/tmp/test.log”);
RandomAccessFile rand=新的RandomAccessFile(文件“rw”);
System.out.println(“文件已创建”);//将字节转换为字符
while(true){
System.out.println(file.lastModified());
System.out.println(file.length());
睡眠(5000);
System.out.println(“完成睡眠”);
int i=(int)rand.length();
rand.seek(0);//查找到文件的起点
对于(int-ct=0;ct
}谁能给我解释一下原因吗?是否与文件的inode有关?基于操作系统的文件系统服务(如创建文件夹、文件、验证权限、更改文件名等)由java.io.file类提供 RandomAccessFile类提供对存储在数据文件中的记录的随机访问。使用这个类,可以进行数据的读写操作。另一个灵活性是,它可以读取和写入基本数据类型,这有助于采用结构化方法处理数据文件
与java.io中的输入和输出流类不同,RandomAccessFile用于读取和写入文件。RandomAccessFile不从InputStream或OutputStream继承。它实现了DataInput和DataOutput接口。主要区别之一是,文件不能直接控制写入或读取,它需要IO流来实现这一点。作为皇家空军,我们可以写或读文件 这里没有证据表明您已经移动或重命名了该文件 如果你不是在节目之外做的,很明显这只是一个时间问题
如果在尝试使用旧名称打开文件之前重命名文件,则该文件将失败。当然这是显而易见的?与
随机访问文件
或者说,输入流
和许多其他java IO功能不同,文件只是一个不可变的句柄,当您需要执行文件系统网关操作时,您可以不时拖动它。您可以将其视为参考:文件实例指向某个指定的路径。另一方面,RandomAccessFile
仅在构建时具有路径的概念:它进入指定的路径,打开文件并获取文件系统描述符——您可以将其视为给定文件的唯一id,它不会在移动和其他一些操作中更改,并在整个生命周期中使用此id来寻址文件 你能更具体地解释为什么上面的案例1和案例2行为不同吗?我在这里更新了我的标题,我更想知道为什么在移动或删除物理文件时它们之间会有差异。java.io.file不会引发任何IOException。你在说什么?@EJP谢谢你指出这一点。我应该把我的测试代码粘贴在第一个位置。我的意思是在文件读取过程中抛出IOExceptions。
public void readRAF() throws IOException, InterruptedException {
File file = new File("/tmp/test.log");
RandomAccessFile rand = new RandomAccessFile(file, "rw");
System.out.println("file created"); // convert byte into char
while (true) {
System.out.println(file.lastModified());
System.out.println(file.length());
Thread.sleep(5000);
System.out.println("finish sleeping");
int i = (int) rand.length();
rand.seek(0); // Seek to start point of file
for (int ct = 0; ct < i; ct++) {
byte b = rand.readByte(); // read byte from the file
System.out.print((char) b); // convert byte into char
}
}