What are file operations in Java?


File class provides various methods to perform respective file operations.

  • canRead(): This method tests whether the application can read the file denoted by this abstract pathname. It returns true if and only if the file specified by this abstract pathname exists and can be read by the application; false otherwise.
  • canWrite(): This method tests whether the application can modify the file denoted by this abstract pathname. It 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.
  • createNewFile(): This method atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. It returns true if the named file does not exist and was successfully created; false if the named file already exists.
  • delete(): This method deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted. It returns true if and only if the file or directory is successfully deleted; false otherwise.
  • exists(): This method tests whether the file or directory denoted by this abstract pathname exists. It returns true if and only if the file or directory denoted by this abstract pathname exists; false otherwise.
  • getAbsolutePath(): This method Returns the absolute pathname string of this abstract pathname.
  • list(): This method returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.
  • renameTo(): This method renames the file denoted by this abstract pathname. It returns true if and only if the renaming succeeded; false otherwise.

Monica Mona
Monica Mona

Student of life, and a lifelong learner

Updated on: 25-Feb-2020

149 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements