Learn Java Programming - File Class Part One Tutorial

Опубликовано: 03 Октябрь 2024
на канале: Daniel Ross
2,121
14

The File class has been around since the very beginning of Java and it is part of the java.io package. The File class contains methods and constructors for various file and directory manipulating features.
Java is platform independent; your Java programs can execute on any operating system that the JVM can run on. That doesn't mean that the JVM is independent of the rules of the file system that the JVM is running on. As a matter of fact, the JVM and your programs must be written in such a way that they can dynamically adapt to the OS that they are being run on.
Consider this Windows hard coded path represented as a string: "c:\\Java\\BW\\Sample.txt". If a user attempted to run your program on a Linux or UNIX OS, your program will fail miserably. That is because the directory separator is '/' in UNIX as opposed to '\' in Windows. The file class provides us with several tools to dynamically create our directory and file structure. On a side note, in Java 7, a new class named Files was introduced to provide greater flexibility and functionality for supporting dynamic directory and file capabilities. I will be creating a tutorial on that class in the future. Don't be tempted to skip over learning the File class as it is necessary to understand some core concepts before tackling the Files class.