Java虚拟机如何处理I/O操作?

Aad*_*hah 4 java io jvm

我正在查看Java字节码指令列表,我注意到没有任何I/O指令.这引起了我的兴趣.JVM如何执行System.out.println不支持I/O指令的方法?

如果它使用某种形式的内存映射I/O,那么它如何与OS通信以读取文件描述符等?JVM是否实现了自己的抽象层来处理I/O操作?Java I/O包(java.io和java.nio)是用C/C++实现的吗?

NPE*_*NPE 5

如果查看库源代码,您将看到所有与低级API(OS等)的接口都是使用本机代码完成的.

例如,采取FileOutputStream:

/**
 * Opens a file, with the specified name, for writing.
 * @param name name of file to be opened
 */
private native void open(String name) throws FileNotFoundException;

/**
 * Writes the specified byte to this file output stream. Implements
 * the <code>write</code> method of <code>OutputStream</code>.
 *
 * @param      b   the byte to be written.
 * @exception  IOException  if an I/O error occurs.
 */
public native void write(int b) throws IOException;

/**
 * Writes a sub array as a sequence of bytes.
 * @param b the data to be written
 * @param off the start offset in the data
 * @param len the number of bytes that are written
 * @exception IOException If an I/O error has occurred.
 */
private native void writeBytes(byte b[], int off, int len) throws IOException;
Run Code Online (Sandbox Code Playgroud)

然后是相应的C文件(通常是特定于操作系统的).

  • 对于那些不懂术语的人来说,"本机代码"是指用Java以外的语言实现的代码. (2认同)