How to Import a Class From API in Java


In this example we will show the method to import a class from the API in Java.

Source Code

package com.beginner.examples;

import java.io.File; // import a class

public class ImportClass {
  public static void main(String[] args) {
      File f = new File("test.txt");
      if (f.exists()) { // judge whether the file exists
        System.out.println("YES");
      } else {
        System.out.println("NO");
      }
  }
}

Output:

NO

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments