🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Files class have the Files.size() method to determine the size of the file. This is the most recent API and it is recommended for new Java applications.
Java file size with Files
The code example determines the file size using the Files' size() method.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class JavaFileSizeExample {
public static void main(String[] args) throws IOException {
String fileName = "src/main/resources/words.txt";
Path filePath = Paths.get(fileName);
long fileSize = Files.size(filePath);
System.out.format("The size of the file: %d bytes", fileSize);
}
}
Output:
The size of the file: 200 bytes
Comments
Post a Comment
Leave Comment