Article image Dart Basics: File Manipulation

3.9. Dart Basics: File Manipulation

Page 27 | Listen in audio

Dart is a modern, object-oriented, highly efficient programming language that was developed by Google. It is used to build high-performance mobile, web, and desktop applications. One of Dart's most notable features is its file manipulation capabilities. In this article, we'll discuss the basics of file manipulation in Dart.

Introduction to File Manipulation in Dart

File manipulation is a crucial aspect of programming. It allows programs to read data from files, write data to files, and manipulate file and directory structure. Dart provides a powerful and easy to use library called 'dart:io' which allows us to perform file I/O operations. To start using the dart:io library, we need to import it into our Dart program.

import 'dart:io';

Reading Files in Dart

To read a file in Dart, we first need to create a 'File' object that points to the file we want to read. We can do this using the 'File' constructor and supplying the path to the file as an argument. Once we have a 'File' object, we can use the 'readAsString()' method to read the contents of the file as a single string.

var file = File('test.txt');
var contents;

if (await file.exists()) {
  contents = await file.readAsString();
  print(contents);
}

Write to Files in Dart

Writing to a Dart file is as simple as reading from one. We can use the 'writeAsString()' method on the 'File' object to write data to a file. If the file does not exist, it will be created automatically.

var file = File('test.txt');
await file.writeAsString('Hello Dart!');

By default, the 'writeAsString()' method overwrites the existing contents of the file. If we want to add to the existing content instead of replacing it, we can pass the 'mode' parameter with the value 'FileMode.append'.

var file = File('test.txt');
await file.writeAsString('Hello again, Dart!', mode: FileMode.append);

Directory Manipulation in Dart

Dart also allows us to work with directories. We can create, move, rename and list the contents of directories. To create a directory, we use the 'create()' method on the 'Directory' object.

var dir = Directory('test');
if (await dir.exists()) {
  print('Directory already exists');
} else {
  await dir.create();
  print('Directory created');
}

To list the contents of a directory, we use the 'list()' method on the 'Directory' object. This method returns a stream of 'FileSystemEntity' objects, which can be files or directories.

var dir = Directory.current;
await for (var entity in dir.list()) {
  print(entity.path);
}

In summary, Dart provides a robust and easy-to-use 'dart:io' library for manipulating files and directories. It allows us to read and write files, as well as manipulate the directory structure, in an efficient and intuitive way.

Now answer the exercise about the content:

What is the purpose of the 'dart:io' library in the Dart programming language?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Dart Basics: Asynchronous Programming

Next page of the Free Ebook:

28Dart Basics: Asynchronous Programming

3 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text