FileObject, JavaFileObjectpublic class SimpleJavaFileObject extends Object implements JavaFileObject
JavaFileObject.Kind
| Modifier and Type | Field | Description |
|---|---|---|
protected final JavaFileObject.Kind |
kind |
The kind of this file object. |
protected final URI |
uri |
A URI for this file object. |
| Modifier | Constructor | Description |
|---|---|---|
protected |
Construct a SimpleJavaFileObject of the given kind and with the given URI. |
| Modifier and Type | Method | Description |
|---|---|---|
boolean |
delete() |
Deletes this file object. |
static JavaFileObject |
forSource |
Creates a JavaFileObject which represents the given source content. |
Modifier |
getAccessLevel() |
Provides a hint about the access level of the class represented by this file object. |
CharSequence |
getCharContent |
Returns the character content of this file object, if available. |
JavaFileObject.Kind |
getKind() |
Returns the kind of this file object. |
long |
getLastModified() |
Returns the time this file object was last modified. |
String |
getName() |
Returns a user-friendly name for this file object. |
NestingKind |
getNestingKind() |
Provides a hint about the nesting level of the class represented by this file object. |
boolean |
isNameCompatible |
Checks if this file object is compatible with the specified simple name and kind. |
InputStream |
openInputStream() |
Returns an InputStream for this file object. |
OutputStream |
openOutputStream() |
Returns an OutputStream for this file object. |
Reader |
openReader |
Returns a reader for this object. |
Writer |
openWriter() |
Returns a Writer for this file object. |
URI |
toUri() |
Returns a URI identifying this file object. |
protected final URI uri
protected final JavaFileObject.Kind kind
protected SimpleJavaFileObject(URI uri, JavaFileObject.Kind kind)
uri - the URI for this file objectkind - the kind of this file objectpublic URI toUri()
FileObjecttoUri in interface FileObject
public String getName()
FileObject"BobsApp\Test.java" on the command line, this method should return
"BobsApp\Test.java" whereas the toUri method might return
file:///C:/Documents%20and%20Settings/UncleBob/BobsApp/Test.java.getName in interface FileObject
public InputStream openInputStream() throws IOException
openInputStream in interface FileObject
IOException - if an I/O error occurredpublic OutputStream openOutputStream() throws IOException
openOutputStream in interface FileObject
IOException - if an I/O error occurredpublic Reader openReader(boolean ignoreEncodingErrors) throws IOException
ignoreEncodingErrors is true.openReader in interface FileObject
getCharContent(boolean) in a Reader.ignoreEncodingErrors - ignore encoding errors if trueIllegalStateException - if this file object was opened for writing and does not support readingUnsupportedOperationException - if this kind of file object does not support character accessIOException - if an I/O error occurredpublic CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException
ignoreEncodingErrors is true.getCharContent in interface FileObject
ignoreEncodingErrors - ignore encoding errors if truenull otherwiseIOException - if an I/O error occurredpublic Writer openWriter() throws IOException
openWriter in interface FileObject
openOutputStream() in a Writer.IllegalStateException - if this file object was opened for reading and does not support writingUnsupportedOperationException - if this kind of file object does not support character accessIOException - if an I/O error occurredpublic long getLastModified()
getLastModified in interface FileObject
0L.0Lpublic boolean delete()
delete in interface FileObject
falsepublic JavaFileObject.Kind getKind()
JavaFileObjectgetKind in interface JavaFileObject
this.kindpublic boolean isNameCompatible(String simpleName, JavaFileObject.Kind kind)
isNameCompatible in interface JavaFileObject
simpleName + kind.extension or if it ends with
"/" + simpleName + kind.extension. This method calls getKind() and toUri() and does not access the fields uri and kind directly.
simpleName - a simple name of a classkind - a kindtrue if this file object is compatible; false otherwisepublic NestingKind getNestingKind()
NestingKind.MEMBER to mean NestingKind.LOCAL or NestingKind.ANONYMOUS. If the nesting level is not known or this file object does not represent a class file this method returns null.getNestingKind in interface JavaFileObject
null.null if the nesting kind is not knownpublic Modifier getAccessLevel()
null.getAccessLevel in interface JavaFileObject
null.public static JavaFileObject forSource(URI uri, String content)
JavaFileObject which represents the given source content. The provided uri will be returned from toUri(). The provided content will be returned from getCharContent(boolean). The getKind() method will return JavaFileObject.Kind.SOURCE.
All other methods will behave as described in the documentation in this class, as if the constructor is called with uri and Kind.SOURCE.
This method can be, for example, used to compile an in-memory String to a set of classfile in a target directory:
var code = """
public class CompiledCode {}
""";
var compiler = ToolProvider.getSystemJavaCompiler();
var targetDirectory = "...";
var task = compiler.getTask(null,
null,
null,
List.of("-d", targetDirectory),
null,
List.of(SimpleJavaFileObject.forSource(URI.create("CompiledCode.java"), code)));
if (!task.call()) {
throw new IllegalStateException("Compilation failed!");
}
uri - that should be used for the resulting JavaFileObject
content - the content of the JavaFileObject
JavaFileObject representing the given source content.
© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/25/docs/api/java.compiler/javax/tools/SimpleJavaFileObject.html