Java Files

www.‮.iruttual‬com
Java Files

The java.io.File class in Java provides a way to represent a file or directory path in a platform-independent manner. You can use this class to perform various operations on files and directories, such as creating, deleting, renaming, and checking if they exist.

Here are some examples of common tasks you can perform with the File class:

Java Create a new file:

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

public class Main {
  public static void main(String[] args) {
    File file = new File("file.txt");
    try {
      file.createNewFile();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
Java Delete a file:
import java.io.File;

public class Main {
  public static void main(String[] args) {
    File file = new File("file.txt");
    file.delete();
  }
}

Java Check if a file exists:

import java.io.File;

public class Main {
  public static void main(String[] args) {
    File file = new File("file.txt");
    if (file.exists()) {
      System.out.println("File exists");
    } else {
      System.out.println("File does not exist");
    }
  }
}

There are many other methods and properties available in the File class that you can use to perform various operations on files and directories. You can find more information about the File class in the Java documentation.

Created Time:2017-11-03 15:57:10  Author:lautturi