在的FileInputStream你會發現這樣的代碼:
public FileInputStream(File file) throws FileNotFoundException {
...
open(name);
}
和開放的定義:
/*
* Opens the specified file for reading.
* @param name the name of the file
*/
private native void open(String name) throws FileNotFoundException;
你可以很容易地檢查你的JDK如何使用JNI根據您的操作系統來執行此操作, Windows使用OpenJdk的例子,你可以在io_util_md.c中找到這段代碼(complete source):
if (h == INVALID_HANDLE_VALUE) {
int error = GetLastError();
if (error == ERROR_TOO_MANY_OPEN_FILES) {
JNU_ThrowByName(env, JNU_JAVAIOPKG "IOException",
"Too many open files");
return -1;
}
throwFileNotFoundException(env, path);
return -1;
}
你可以檢查throwFileNotFoundException的io_util.c(complete code)執行:
void
throwFileNotFoundException(JNIEnv *env, jstring path)
{
char buf[256];
jint n;
jobject x;
jstring why = NULL;
n = JVM_GetLastErrorString(buf, sizeof(buf));
if (n > 0) {
why = JNU_NewStringPlatform(env, buf);
}
x = JNU_NewObjectByName(env,
"java/io/FileNotFoundException",
"(Ljava/lang/String;Ljava/lang/String;)V",
path, why);
if (x != NULL) {
(*env)->Throw(env, x);
}
}
我現在能理解葉蘭。謝謝 – Sanjeevan