An abstract representation of file and directory pathnames.
File类是 文件或者目录 的路径名称(pathname)的抽象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
publicclassFileimplementsSerializable, Comparable<File> { /** * This abstract pathname's normalized pathname string. A normalized * pathname string uses the default name-separator character and does not * contain any duplicate or redundant separators. */ private String path;
/** * The length of this abstract pathname's prefix, or zero if it has no * prefix. */ privatetransientint prefixLength;
An optional system-dependent prefix string, such as a disk-drive specifier, “/“ for the UNIX root directory, or “\\“ for a Microsoft Windows UNC pathname
相对路径,则是相对于一个默认的 work directory. 默认情况下,io包里的类,将把相对路径解析成相对于 current user directory 的路径。而 current user directory 则是由 系统属性 user.dir 来设置的。通常情况下这个路径是 JVM 被调用的那个目录。
JNIEXPORT jboolean JNICALL Java_java_io_WinNTFileSystem_createFileExclusively(JNIEnv *env, jclass cls, jstring path) { HANDLE h = NULL; WCHAR *pathbuf = pathToNTPath(env, path, JNI_FALSE); if (pathbuf == NULL) return JNI_FALSE; // The CreateFile function creates or opens a file, file stream, // directory, physical disk, volume, console buffer, tape drive, // communications resource, mailslot, or named pipe. h = CreateFileW( pathbuf, /* Wide char path name */ GENERIC_READ | GENERIC_WRITE, /* Read and write permission */ FILE_SHARE_READ | FILE_SHARE_WRITE, /* File sharing flags */ NULL, /* Security attributes */ CREATE_NEW, /* creation disposition */ FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OPEN_REPARSE_POINT, /* flags and attributes */ NULL);
if (h == INVALID_HANDLE_VALUE) { DWORD error = GetLastError(); if ((error != ERROR_FILE_EXISTS) && (error != ERROR_ALREADY_EXISTS)) { // return false rather than throwing an exception when there is // an existing file. DWORD a = GetFileAttributesW(pathbuf); if (a == INVALID_FILE_ATTRIBUTES) { SetLastError(error); JNU_ThrowIOExceptionWithLastError(env, "Could not open file"); } } free(pathbuf); return JNI_FALSE; } free(pathbuf); CloseHandle(h); return JNI_TRUE; }
JNIEXPORT jboolean JNICALL Java_java_io_WinNTFileSystem_createDirectory(JNIEnv *env, jobject this, jobject file) { BOOL h = FALSE; WCHAR *pathbuf = fileToNTPath(env, file, ids.path); if (pathbuf == NULL) { /* Exception is pending */ return JNI_FALSE; } // The CreateDirectory function creates a new directory. h = CreateDirectoryW(pathbuf, NULL); free(pathbuf);