我在ubuntu16.04上使用java-1.8.0-openjdk-amd64
我试图理解在附加时文件会发生什么
假设我附加到一个非常大的文件,java是否将整个文件加载到内存中?如何查看本机呼叫?
从java.io.fileoutputstream.java
/**
* Opens a file, with the specified name, for overwriting or appending.
* @param name name of file to be opened
* @param append whether the file is to be opened in append mode
*/
private native void open0(String name, boolean append)
throws FileNotFoundException;
// wrap native call to allow instrumentation
/**
* Opens a file, with the specified name, for overwriting or appending.
* @param name name of file to be opened
* @param append whether the file is to be opened in append mode
*/
private void open(String name, boolean append)
throws FileNotFoundException {
open0(name, append);
}
更新:
查看jdk8源代码
src/solaris/native/sun/nio/ch/inheritedchannel.c:java\u sun\u nio\u ch\u inheritedchannel\u open0(jnienv*env,jclass cla,jstring path,jint of lag)
JNIEXPORT jint JNICALL
Java_sun_nio_ch_InheritedChannel_open0(JNIEnv *env, jclass cla, jstring path, jint oflag)
{
const char* str;
int oflag_actual;
/* convert to OS specific value */
switch (oflag) {
case sun_nio_ch_InheritedChannel_O_RDWR :
oflag_actual = O_RDWR;
break;
case sun_nio_ch_InheritedChannel_O_RDONLY :
oflag_actual = O_RDONLY;
break;
case sun_nio_ch_InheritedChannel_O_WRONLY :
oflag_actual = O_WRONLY;
break;
default :
JNU_ThrowInternalError(env, "Unrecognized file mode");
return -1;
}
str = JNU_GetStringPlatformChars(env, path, NULL);
if (str == NULL) {
return (jint)-1;
} else {
int fd = open(str, oflag_actual);
if (fd < 0) {
JNU_ThrowIOExceptionWithLastError(env, str);
}
JNU_ReleaseStringPlatformChars(env, path, str);
return (jint)fd;
}
}
1条答案
按热度按时间f4t66c6m1#
不,它不会将文件加载到内存中。这样就不可能附加到大文件中。
实际逻辑取决于所使用的文件系统,但基本上只需要加载文件的最后一个块,用附加数据重写,并为任何附加数据编写附加块。
你不用担心。如果你真的想担心它,学习文件系统是如何工作的。