FileChannel Size Example
This program demonstrates getting size of a file using FileChannel.
Problem Statement
Write a Java program that prints size of a file using FileChannel.size().
Source Code
| 1 | import java.io.*; |
| 2 | import java.nio.channels.*; |
| 3 | import java.nio.file.*; |
| 4 | public class FileChannelSizeExample { |
| 5 | public static void main(String[] args) throws Exception { |
| 6 | System.out.println("Eduinq FileChannel Size Example"); |
| 7 | FileChannel channel=FileChannel.open(Paths.get("sample.txt"),StandardOpenOption.READ); |
| 8 | long size=channel.size(); |
| 9 | System.out.println("Eduinq File Size: "+size+" bytes"); |
| 10 | channel.close(); |
| 11 | } |
| 12 | } |
Program Output
Eduinq FileChannel Size Example Eduinq File Size: 42 bytes
Explanation
Uses FileChannel.size() to get file length. - Returns size in bytes. - Exception handling ensures safe operations. - In enterprise systems, file size helps manage storage. Conclusion: Shows getting file size.