• Profile
  • Resume
  • Portfolio
  • Blog
Avatar

Nelson Silva

A human. Being. Coffee lover.

Blog

I Write My Thoughts Here

File Class in Java

March 30, 2022 Java

In this post, you will learn how to work with the File class so that you can use that same knowledge and thus evolve.

  • this class provides methods for creating, deleting, getting the absolute path, and checking for a single file at a time.
File (object).createNewFile() Create a file in the specified path
File (object).delete() Deletion of a certain file
File (object).getAbsolutePath() Gets the absolute path of the file
File (object).exists() Checks if the data file exists or not
package com.caffeinealgorithm.programminginjava;

import java.io.File;
import java.io.IOException;

public class FileClass {
  private File file = new File("File.txt");

  public void Run() {
    /*
      try {
        file.createNewFile();
      }
      catch (IOException exception) {
        System.out.println(exception);
      }
    */

    file.delete();

    // System.out.println(file.getAbsolutePath());

    if (file.exists())
      System.out.println("The file \"File.txt\" exists.");
    else
      System.out.println("The file \"File.txt\" does not exist.");
  }
}

// The file "File.txt" does not exist.

Don’t forget to watch the video and you can always read this post in Portuguese.

Happy coding!

Previous Next

Categories

  • Announcements
  • Python
  • C#
  • Java
  • Go

Latest Posts

  • for Loop in Go
    May 5, 2022
  • Switch in Go
    May 4, 2022
  • Comparison Operators in Go
    May 3, 2022
+351913416022
contact@nelsonsilvadev.com
Porto, Portugal
Nelson Silva © 2022 All Rights Reserved