Java 6 api file




















Rather, JavaFileManager manages all the files and their contents in the form of FileObject and JavaFileObject which represents the abstract representation for any kind of files by managed by the JavaFileManager.

FileObject and JavaFileObject refer to the abstract file representation that are being managed by the JavaFileManager and also have support related to reading and writing the contents to the right destination. The only difference between a FileObject and a JavaFileObject is that a FileObject can represent any kind of FileObject like text file, properties file, image file etc , whereas a JavaFileObject can represent only java source file.

If a FileObject represents a java source file or a class file, then implementations should take care to return a JavaFileObject instead. The second dependent service by the JavaCompiler is the Diagnostic Service. Diagnostic usually refers to errors, warnings or informative messages that may appear in a program.

For getting diagnostics messages during the compilation process, we can attach a listener to the compiler object. The listener is a DiagnosticListener object and its report method will be called with a diagnostic method which contains many a more information like the kind of diagnostics error, warning, information or other , the source for this diagnostics, the line number in the source code, a descriptive message etc.

Before going into the sample code to clarify things like JavaFileManager, Diagnostic,and the DiagnosticListener classes, let us have a quick review on the class CompilationTask. From its name, obviously one can tell that it represents an object that encapsulates the actual compilation operation. We can initiate the compilation operation by calling the call method in the CompilationTask object. Let we now see the parameters that are needed to pass to the getCompilationTask … method.

Almost all the arguments can be null with their defaults, which is discussed later , but the final argument represents the list of java objects to be compiled or the compilation units which cannot be null. It has one method called iterator which returns an Iterator object, and using it one can traverse over the collection by using the combinational hasNext and the next methods.

Considering type safety which started from Java 5. With more bits of theory in the last few sections, let us see a sample program that incorporates all the classes and the methods that we saw listed. Purposefully we create a java file called MoreErrors. The listing for MoreErrors.

Let us look into the AdvancedCompilationTest. Line 1 is essentially creating an object of type JavaCompiler using the ToolProvider class. This is the entry point.

To rewind the theory a bit, JavaFileManager object is used to manage the input and the output files that a tool normally deals with. Line 2 creates an object of Type DiagnosticListener, since we want to monitor the diagnostics that may happen during the process of compilation diagnostics, in the form of error messages will always happen in our case, as we have purposefully done some errors in the java code.

We have overridden the report Diagnostic method and have extracted all the possible information. Since diagnostics can happen in any kind of file object, how can we specifically tell that this Diagnostics is for a java file object? The answer has become easy because of Java 5.

One can notice that MyDiagnosticListener is a typed class having some typed parameter meaning that it can act on any kind of object that has diagnostics properties; here we are explicitly telling that the diagnostics is for JavaFileObject and not for any other file object by mentioning the JavaFileObject in the class declaration and the method declaration shown in bold.

In Line 3, we are associating the Diagnostics listener object to the compiler object through the standard java file manager by making this method call. This method tells to attach the diagnostics listener object to this compiler object, so whenever this compiler object executes a compilation operation, and if any diagnostics related errors or warnings have occurred in a program, then the diagnostics being encapsulated by the Diagnostic object will be passed back to the report method of the DiagnosticListener interface.

The last 2 arguments refer the locale and charset arguments for formatting the diagnostic messages in a particular locale and by using the specified charset which can be null. Line 4 and Line 5 populates the file objects to be compiled by using the convenience objects in the StandardJavaFileManager class. Line 6 gets an instance of the CompilationTask object by calling the getCompilationTask method and by passing the fileManager, listener and the fileObjects objects.

The null arguments refer to the Writer object for getting the output from the compiler , list of options the options that we pass to javac like —classpath classes, -sourcepath Sources… and classes for processing the custom annotations that are found in the source code.

Finally, the actual compilation operation is done by calling the call method which returns true if all the files compilationUnits succeed compilation. If any of the files have errors in it , then the call method will return false. Anyway, in our case, we have some error code in the MoreErrors.

In the previous program, we saw that we have a customized class called MyDiagnosticListener. Its sole purpose it to collect and print all the diagnostic messages to the console. It has a method called getDiagnostics which returns a list , through which we can iterate and can output the diagnostic messages to the console. Provides interfaces for tools which can be invoked from a program, for example, compilers. Provides a runtime binding framework for client applications including unmarshalling, marshalling, and validation capabilities.

File []. File File parent, String child Creates a new File instance from a parent abstract pathname and a child pathname string. FileInputStream File file Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system. FileOutputStream File file Creates a file output stream to write to the file represented by the specified File object.

FileOutputStream File file, boolean append Creates a file output stream to write to the file represented by the specified File object. PrintStream File file Creates a new print stream, without automatic line flushing, with the specified file. PrintStream File file, String csn Creates a new print stream, without automatic line flushing, with the specified file and charset.

PrintWriter File file Creates a new PrintWriter, without automatic line flushing, with the specified file. Returns: The name of the file or directory denoted by this abstract pathname, or the empty string if this pathname's name sequence is empty getParent public String getParent Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory. The parent of an abstract pathname consists of the pathname's prefix, if any, and each name in the pathname's name sequence except for the last.

If the name sequence is empty then the pathname does not name a parent directory. Returns: The pathname string of the parent directory named by this abstract pathname, or null if this pathname does not name a parent getParentFile public File getParentFile Returns the abstract pathname of this abstract pathname's parent, or null if this pathname does not name a parent directory.

Returns: The abstract pathname of the parent directory named by this abstract pathname, or null if this pathname does not name a parent Since: 1. The resulting string uses the default name-separator character to separate the names in the name sequence. Returns: The string form of this abstract pathname isAbsolute public boolean isAbsolute Tests whether this abstract pathname is absolute. The definition of absolute pathname is system dependent. Returns: true if this abstract pathname is absolute, false otherwise getAbsolutePath public String getAbsolutePath Returns the absolute pathname string of this abstract pathname.

If this abstract pathname is already absolute, then the pathname string is simply returned as if by the getPath method. If this abstract pathname is the empty abstract pathname then the pathname string of the current user directory, which is named by the system property user. Otherwise this pathname is resolved in a system-dependent way. On UNIX systems, a relative pathname is made absolute by resolving it against the current user directory. On Microsoft Windows systems, a relative pathname is made absolute by resolving it against the current directory of the drive named by the pathname, if any; if not, it is resolved against the current user directory.

Returns: The absolute pathname string denoting the same file or directory as this abstract pathname Throws: SecurityException - If a required system property value cannot be accessed. Equivalent to new File this.

Returns: The absolute abstract pathname denoting the same file or directory as this abstract pathname Throws: SecurityException - If a required system property value cannot be accessed. Since: 1. A canonical pathname is both absolute and unique.

The precise definition of canonical form is system-dependent. This method first converts this pathname to absolute form if necessary, as if by invoking the getAbsolutePath method, and then maps it to its unique form in a system-dependent way. This typically involves removing redundant names such as ". Every pathname that denotes an existing file or directory has a unique canonical form.

Every pathname that denotes a nonexistent file or directory also has a unique canonical form. The canonical form of the pathname of a nonexistent file or directory may be different from the canonical form of the same pathname after the file or directory is created.

Similarly, the canonical form of the pathname of an existing file or directory may be different from the canonical form of the same pathname after the file or directory is deleted. FileDescriptor method denies read access to the file Since: 1. Converts this abstract pathname into a file: URL. The exact form of the URL is system-dependent. If it can be determined that the file denoted by this abstract pathname is a directory, then the resulting URL will end with a slash. The exact form of the URI is system-dependent.

If it can be determined that the file denoted by this abstract pathname is a directory, then the resulting URI will end with a slash. For a given abstract pathname f , it is guaranteed that new File f. Due to the system-dependent nature of abstract pathnames, however, this relationship typically does not hold when a file: URI that is created in a virtual machine on one operating system is converted into an abstract pathname in a virtual machine on a different operating system.

Returns: An absolute, hierarchical URI with a scheme equal to "file" , a path representing this abstract pathname, and undefined authority, query, and fragment components Throws: SecurityException - If a required system property value cannot be accessed. Returns: true if and only if the file specified by this abstract pathname exists and can be read by the application; false otherwise Throws: SecurityException - If a security manager exists and its SecurityManager.

String method denies read access to the file canWrite public boolean canWrite Tests whether the application can modify the file denoted by this abstract pathname.

Returns: true if and only if the file system actually contains a file denoted by this abstract pathname and the application is allowed to write to the file; false otherwise. String method denies write access to the file exists public boolean exists Tests whether the file or directory denoted by this abstract pathname exists. Returns: true if and only if the file or directory denoted by this abstract pathname exists; false otherwise Throws: SecurityException - If a security manager exists and its SecurityManager.

String method denies read access to the file or directory isDirectory public boolean isDirectory Tests whether the file denoted by this abstract pathname is a directory.

Returns: true if and only if the file denoted by this abstract pathname exists and is a directory; false otherwise Throws: SecurityException - If a security manager exists and its SecurityManager. String method denies read access to the file isFile public boolean isFile Tests whether the file denoted by this abstract pathname is a normal file.

A file is normal if it is not a directory and, in addition, satisfies other system-dependent criteria. Any non-directory file created by a Java application is guaranteed to be a normal file. Returns: true if and only if the file denoted by this abstract pathname exists and is a normal file; false otherwise Throws: SecurityException - If a security manager exists and its SecurityManager. String method denies read access to the file isHidden public boolean isHidden Tests whether the file named by this abstract pathname is a hidden file.

The exact definition of hidden is system-dependent. On UNIX systems, a file is considered to be hidden if its name begins with a period character '. On Microsoft Windows systems, a file is considered to be hidden if it has been marked as such in the filesystem.

Returns: true if and only if the file denoted by this abstract pathname is hidden according to the conventions of the underlying platform Throws: SecurityException - If a security manager exists and its SecurityManager. String method denies read access to the file Since: 1. String method denies read access to the file length public long length Returns the length of the file denoted by this abstract pathname.

The return value is unspecified if this pathname denotes a directory. Returns: The length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist. Some operating systems may return 0L for pathnames denoting system-dependent entities such as devices or pipes. String method denies read access to the file createNewFile public boolean createNewFile throws IOException Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.

The check for the existence of the file and the creation of the file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the file. Note: this method should not be used for file-locking, as the resulting protocol cannot be made to work reliably. The FileLock facility should be used instead. String method denies write access to the file Since: 1. If this pathname denotes a directory, then the directory must be empty in order to be deleted.

Returns: true if and only if the file or directory is successfully deleted; false otherwise Throws: SecurityException - If a security manager exists and its SecurityManager. String method denies delete access to the file deleteOnExit public void deleteOnExit Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates.

Files or directories are deleted in the reverse order that they are registered. Invoking this method to delete a file or directory that is already registered for deletion has no effect.

Deletion will be attempted only for normal termination of the virtual machine, as defined by the Java Language Specification. Once deletion has been requested, it is not possible to cancel the request.

This method should therefore be used with care. String method denies delete access to the file Since: 1.

If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of strings is returned, one for each file or directory in the directory.

Names denoting the directory itself and the directory's parent directory are not included in the result. Each string is a file name rather than a complete path. There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.

Returns: An array of strings naming the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. String method denies read access to the directory list public String [] list FilenameFilter filter Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.

The behavior of this method is the same as that of the list method, except that the strings in the returned array must satisfy the filter. If the given filter is null then all names are accepted. Otherwise, a name satisfies the filter if and only if the value true results when the FilenameFilter. File, java. String method of the filter is invoked on this abstract pathname and the name of a file or directory in the directory that it denotes.

Parameters: filter - A filename filter Returns: An array of strings naming the files and directories in the directory denoted by this abstract pathname that were accepted by the given filter. The array will be empty if the directory is empty or if no names were accepted by the filter.

String method denies read access to the directory listFiles public File [] listFiles Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname. Otherwise an array of File objects is returned, one for each file or directory in the directory. Pathnames denoting the directory itself and the directory's parent directory are not included in the result.



0コメント

  • 1000 / 1000