例子
以下示例显示java.io.File.valid()方法的用法。
package com.jc2182;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) throws IOException {
FileInputStream fis = null;
FileDescriptor fd = null;
boolean bool = false;
try {
// create input stream
fis = new FileInputStream("c:/java test.txt");
// get file descriptor
fd = fis.getFD();
// tests file descriptor object's validity
bool = fd.valid();
// print
System.out.print("is file descriptor valid?: "+bool);
} catch(Exception e) {
// if any error occurs
e.printStackTrace();
} finally {
// releases systems resources
if(fis!=null)
fis.close();
}
}
}
让我们编译并运行以上程序,这将产生以下结果-
is file descriptor valid?: true