Class File

java.lang.Object
com.codename1.io.File

public class File extends Object
This class provides a similar API to java.io.File making it almost into a "drop in" replacement. It is placed in a different package because it is incompatible to java.io.File by definition. It is useful in getting some simple code to work without too many changes
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Interface for filtering files.
    static interface 
    Interface to filter filenames.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
     
    static final char
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    File(File parent, String path)
    Creates a new file in the given parent directory, and subpath.
    File(String path)
    Creates a new file object with given path.
    File(String dir, String file)
    Creates a new file object in a given directory.
    File(URI uri)
    Creates a new File object from the given URI
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Checks if this file is executable.
    boolean
    Creates this file as a new blank file in the file system.
    static File
    createTempFile(String prefix, String suffix)
    Creates a temporary file.
    boolean
    Deletes the file described by this object on the file system.
    boolean
    Checks if the given object refers to the same file.
    boolean
    Checks if the file described by this object exists on the file system.
    Gets the absolute file - which is always itself, since #isAbsolute() always returns true.
    Gets the absolute path of the file as a string,
    long
    Gets the free space on the root file system.
    Returns the file name.
    Gets the parent directory path.
    Returns the file object for the parent directory.
    Gets the path to the file.
    long
    Returns the total space on the root file system.
    long
    Gets the usable space on this file system.
    int
     
    boolean
    Checks if the path is absolute.
    boolean
    Checks if this file is a directory.
    boolean
    Checks if this file object represents a regular file.
    boolean
    Checks if this is a hidden file.
    long
    Gets the last modified time as a unix timestamp in milliseconds.
    long
    Gets the file size in bytes.
    Returns the list of child files of this directory.
    Returns list of child files of this directory
    Gets a list of child files of this directory.
    Gets a list of child files of this directory, filtering them using the provided filter.
    Gets a list of child files of this directory, filtered using the provided filter.
    static File[]
    List the file system roots.
    boolean
    Attempts to make the directory described by this object.
    boolean
    Attempts to make the directory (and all parent directories) of this object.
    boolean
    Renames the file to the provided file object.
     
    Converts this file to a URI.
    Converts this file to a URL.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • File

      public File(URI uri)

      Creates a new File object from the given URI

      Parameters
      • uri
    • File

      public File(String path)

      Creates a new file object with given path. Paths that do not begin with the "file:" prefix will automatically be prefixed with the app home path.

      Parameters
      • path: The path of the file. Relative or absolute.
    • File

      public File(String dir, String file)

      Creates a new file object in a given directory.

      Parameters
      • dir: The parent directory path.

      • file: The file name

    • File

      public File(File parent, String path)

      Creates a new file in the given parent directory, and subpath.

      Parameters
      • parent: The parent directory.

      • path: The subpath, beginning with the parent directory.

  • Method Details

    • listRoots

      public static File[] listRoots()
      List the file system roots.
    • createTempFile

      public static File createTempFile(String prefix, String suffix) throws IOException

      Creates a temporary file.

      Parameters
      • prefix: The file name prefix.

      • suffix: The file name suffix

      Returns

      The resulting temporary file.

      Throws
      • IOException
      Throws:
      IOException
    • getName

      public String getName()

      Returns the file name.

      Returns

      The file name.

    • getParent

      public String getParent()

      Gets the parent directory path.

      Returns

      The parent directory path.

    • getParentFile

      public File getParentFile()
      Returns the file object for the parent directory.
    • getPath

      public String getPath()
      Gets the path to the file.
    • isAbsolute

      public boolean isAbsolute()
      Checks if the path is absolute. This always returns true as all File objects use absolute paths - even if they were created with relative paths. Relative paths are automatically prefixed with the app home directory path.
    • getAbsolutePath

      public String getAbsolutePath()
      Gets the absolute path of the file as a string,
    • getAbsoluteFile

      public File getAbsoluteFile()

      Gets the absolute file - which is always itself, since #isAbsolute() always returns true.

      Returns

      The same file object.

    • exists

      public boolean exists()
      Checks if the file described by this object exists on the file system.
    • isDirectory

      public boolean isDirectory()
      Checks if this file is a directory.
    • isFile

      public boolean isFile()
      Checks if this file object represents a regular file.
    • isHidden

      public boolean isHidden()
      Checks if this is a hidden file.
    • lastModified

      public long lastModified()
      Gets the last modified time as a unix timestamp in milliseconds.
    • length

      public long length()

      Gets the file size in bytes.

      Returns

      The file size in bytes.

    • createNewFile

      public boolean createNewFile() throws IOException

      Creates this file as a new blank file in the file system.

      Returns

      True if it succeeds.

      Throws
      • IOException
      Throws:
      IOException
    • delete

      public boolean delete()

      Deletes the file described by this object on the file system.

      Returns

      True if delete succeeds.

    • list

      public String[] list()
      Returns the list of child files of this directory.
    • list

      public String[] list(File.FilenameFilter filter)

      Returns list of child files of this directory

      Parameters
      • filter
    • listFiles

      public File[] listFiles()
      Gets a list of child files of this directory.
    • listFiles

      public File[] listFiles(File.FilenameFilter ff)

      Gets a list of child files of this directory, filtered using the provided filter.

      Parameters
      • ff: The filter to use.
    • listFiles

      public File[] listFiles(File.FileFilter ff)

      Gets a list of child files of this directory, filtering them using the provided filter.

      Parameters
      • ff: The filter to use to filter output.
    • mkdir

      public boolean mkdir()

      Attempts to make the directory described by this object.

      Returns

      True on success.

    • mkdirs

      public boolean mkdirs()

      Attempts to make the directory (and all parent directories) of this object.

      Returns

      True on success.

    • renameTo

      public boolean renameTo(File f)

      Renames the file to the provided file object.

      Parameters
      • f: The file object that we are renaming the file to.
      Returns

      True on success.

    • canExecute

      public boolean canExecute()
      Checks if this file is executable.
    • getTotalSpace

      public long getTotalSpace()
      Returns the total space on the root file system.
    • getFreeSpace

      public long getFreeSpace()
      Gets the free space on the root file system.
    • getUsableSpace

      public long getUsableSpace()
      Gets the usable space on this file system.
    • equals

      public boolean equals(Object o)

      Checks if the given object refers to the same file.

      Parameters
      • o
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toURL

      public URL toURL() throws MalformedURLException

      Converts this file to a URL.

      Throws
      • MalformedURLException
      Throws:
      MalformedURLException
    • toURI

      public URI toURI() throws URISyntaxException

      Converts this file to a URI.

      Throws
      • URISyntaxException
      Throws:
      URISyntaxException