2017-09-04 48 views
-1

我使用Java的1.8.0-的OpenJDK-AMD64過的Ubuntu 16.04的Java IO附加到文件實現

我試圖瞭解發生了什麼文件,而我追加

讓我們假設我m附加到一個非常大的文件,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 /本地/陽光/ NIO/CH/InheritedChannel。 c:Java_sun_nio_ch_InheritedChannel_open0(JNIEnv * env,jclass cla,jstring path,jint oflag)

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; 
    } 
} 
+0

https://stackoverflow.com/questions/12594046/java-native-method-source-code – Tom

回答

2

不,它不會將文件加載到內存中。這將使它無法附加到大文件。

實際的邏輯取決於正在使用的文件系統,但基本上只需要加載文件的最後一個塊,用附加數據重寫併爲附加數據寫入附加塊。

你不需要擔心它。如果您確實想擔心它,請了解文件系統的工作方式。

+0

我想知道哪些系統調用java調用,所以我可以瞭解它們,並瞭解如果JVM加載「最後一塊「是指最後一行?如果我有一個只有5行的1GB文件?它加載250MB塊到RAM? – IddoE

+1

閱讀有關文件系統如何工作的信息。 Java與此無關,而塊不是行。 – Kayaman

+0

+1,但我不能接受爲答案,請提供有關附加和寫入文件的參考(如果我可能要求) – IddoE