Free Ebook cover How to create apps from scratch to advanced using Flutter and Dart complete course

How to create apps from scratch to advanced using Flutter and Dart complete course

5

(4)

267 pages

Dart Basics: File Manipulation

Capítulo 27

Estimated reading time: 3 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

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'.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

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.

The dart:io library in Dart is primarily used to perform file input/output (I/O) operations and manage the structure of files and directories. This includes reading and writing files, as well as directory manipulation.

Next chapter

Dart Basics: Asynchronous Programming

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.