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:
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(); } } }
import java.io.File; public class Main { public static void main(String[] args) { File file = new File("file.txt"); file.delete(); } }
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.