Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do the JVM and Java implement reading from and writing to files? [closed]

Tags:

java

file

jvm

In Java, you can read from and write to files. But the JVM can run on many systems that may have different ways of storing files and related data. How does one write JVM code that works on every system?

At what level are native methods used? Does the JVM have a certain set of file functions that must be implemented for each system, and which are then can be called by any language? Or does each language that runs on the JVM (like Java, or Scala) have to do it itself?

like image 246
Tespa42 Avatar asked Jun 12 '13 00:06

Tespa42


2 Answers

Typically, you would write the JVM in a high-level language like C++ and then use that language's provided libraries for interfacing with the file system. You can then compile the JVM on different platforms, OSes, and architectures and leave the responsibility of determining how to do file I/O to the high-level language compiler.

Alternatively, for certain operations, the JVM might have different implementations of file I/O or windowing based on the operating system it runs on. There would then be multiple different implementations, and when building the JVM on a system the compiler can determine which one to use based on a config script, or #ifdefs, etc.

Hope this helps!

like image 118
templatetypedef Avatar answered Sep 19 '22 04:09

templatetypedef


Most IO functions are pretty standard and there are POSIX calls which work across multiple operating systems. Where there is a differences, the relevant code can be included or not using the C pre-processor with #ifndef and #ifndef

like image 33
Peter Lawrey Avatar answered Sep 19 '22 04:09

Peter Lawrey