Working with Files in Dart: How to Read and Write Data Using the File Class

Gustavo Zeloni
3 min readApr 26, 2023

If you’re working on a project that requires file I/O operations in Dart, the dart:io package provides a set of classes that allow you to perform various operations on files and directories. One of the key classes in this package is the File class, which allows you to read and write data to files.

In this article, we’ll explore the basics of file I/O in Dart using the File class. We'll cover how to read data from a file, write data to a file, and append data to a file.

Reading Data from a File

Reading data from a file is a common task in many applications. In Dart, you can use the readAsString() method of the File class to read the entire contents of a file as a string. Here's an example:

import 'dart:io';

void main() {
final file = File('example.txt');
final contents = file.readAsStringSync();
print(contents);
}

In this example, we create a new instance of the File class with the filename 'example.txt'. We then use the readAsStringSync() method to read the contents of the file as a string, and store the result in the contents variable. Finally, we print the contents to the console.

Writing Data to a File

To write data to a file, you can use the writeAsString() method of the File class. This method overwrites any existing data in the file with the new data you provide. Here's an example:

import 'dart:io';

void main() {
final file = File('example.txt');
final contents = 'Hello, world!';
file.writeAsStringSync(contents);
print('Data written to file successfully!');
}

In this example, we create a new instance of the File class with the filename 'example.txt'. We then define a new string contents with the value 'Hello, world!'. We use the writeAsStringSync() method to write the contents of the contents string to the file. Finally, we print a message to the console to confirm that the data was written to the file successfully.

Appending Data to a File

Sometimes you may want to add new data to the end of an existing file without overwriting its contents. To do this, you can use the writeAsString() method with the FileMode.append parameter. Here's an example:

import 'dart:io';

void main() {
final file = File('example.txt');
final contents = '\nNew data to add';
file.writeAsStringSync(contents, mode: FileMode.append);
print('Data appended to file successfully!');
}

In this example, we create a new instance of the File class with the filename 'example.txt'. We define a new string contents with the value '\nNew data to add', which includes a newline character (\n) at the beginning to add the new data to a new line. We use the writeAsStringSync() method with the FileMode.append parameter to append the contents of the contents string to the end of the file. Finally, we print a message to the console to confirm that the data was appended to the file successfully.

Conclusion

In this article, we’ve covered the basics of file I/O using the File class from the dart:io package in Dart. We learned how to read data from a file, write data to a file, and append data to a file. With this knowledge, you can start working on more complex file I/O tasks in your Dart projects.

Keep in mind that the File class provides many other methods and properties that allow you to perform various file I/O operations, such as creating and deleting files, checking if a file exists, and more. Be sure to check out the official Dart documentation for the dart:io package for more information and examples.

I hope you found this article helpful! If you have any questions or feedback, feel free to leave a comment below.

--

--