#DartProjects
Learn the basics of Dart by building projects. Learn to build various projects step by step using Dart along the way brush up your Dart programming skills.
back to Dart projectsProject 12
In this blog post, we’ll explore a simple yet effective program written in Dart that calculates the size of a file or directory. This guide is tailored for junior developers who are getting familiar with Dart and file system operations.
The program we’re discussing is a Dart script that reads a file path from the standard input and computes its size in kilobytes. It works with both files and directories, calculating the size of directories by summing the sizes of all files within.
import 'dart:io';
Dart’s dart:io
library is crucial for file and directory manipulation, providing the necessary classes and methods.
Future<void> main() async {
final path = stdin.readLineSync()!;
final file = File(path);
// Further code
}
The main
function is asynchronous, marked by async
, because it involves IO operations that are inherently asynchronous.
File
object. This object represents the file or directory at the given path.if (await file.exists()) {
final size = await getFileSize(file);
final sizeInKb = size / 1024;
print('Size of $path is ${sizeInKb.toStringAsFixed(2)} KB');
} else {
print('$path does not exist');
}
getFileSize
is called to get the size.getFileSize
FunctionFuture<int> getFileSize(FileSystemEntity entity) async {
// Implementation
}
This function is the core of our program, handling the size calculation for both files and directories.
if (await entity is File) {
return (await (entity as File).length());
}
For files, it simply returns the size using the length
method.
else if (await entity is Directory) {
int size = 0;
await for (final FileSystemEntity subEntity in (entity as Directory).list(recursive: true)) {
if (await subEntity is File) {
size += await (subEntity as File).length();
}
}
return size;
}
For directories:
else {
throw FileSystemException('Unsupported entity type');
}
If the entity is neither a file nor a directory, an exception is thrown.
///
/// File Size Calculator
///
import 'dart:io';
Future<void> main() async {
final path = stdin.readLineSync()!;
final file = File(path);
if (await file.exists()) {
final size = await getFileSize(file);
final sizeInKb = size / 1024;
print('Size of $path is ${sizeInKb.toStringAsFixed(2)} KB');
} else {
print('$path does not exist');
}
}
Future<int> getFileSize(FileSystemEntity entity) async {
if (await entity is File) {
return (await (entity as File).length());
} else if (await entity is Directory) {
int size = 0;
await for (final FileSystemEntity subEntity in (entity as Directory).list(recursive: true)) {
if (await subEntity is File) {
size += await (subEntity as File).length();
}
}
return size;
} else {
throw FileSystemException('Unsupported entity type');
}
}
This Dart program is a practical example of file system manipulation. It’s a great starting point for junior developers to understand asynchronous programming, file handling, and iteration in Dart. Always remember, the key to mastering a new programming language is practice and exploration. Happy coding!
Enjoying? Tell your friends.
Learn the basics of Dart by building projects. Learn to build various projects step by step using Dart along the way brush up your Dart programming skills.
back to Dart projectsJoin our community on Discord to connect with fellow learners, share your progress, and get help with any questions you may have throughout the #DartProject challenge. Join now and get started on your journey to mastering Dart!