Java - File Input Output File Object

Introduction

An object of the File class represents a path of a file or a directory in a platform-independent manner.

You can create a File object from

  • A pathname
  • A parent pathname and a child pathname
  • A URI (uniform resource identifier)

Use one of the following constructors of the File class to create a file:

File(String pathname)
File(File parent, String child)
File(String parent, String child)
File(URI uri)

If you have a file pathname string of dummy.txt, you can create a File object, like so:

File myFile = new File("dummy.txt");

A file named dummy.txt does not have to exist to create a File object using this statement.

The myFile object represents an abstract pathname, which may or may not exist.

Related Topics

Exercise