Read File in Dart
Introduction To File Handling
File handling is an important part of any programming language. In this section, you will learn how to read the file in a dart programming language.
Read File In Dart
Assume that you have a file named test.txt
in the same directory of your dart program.
Welcome to test.txt file.
This is a test file.
Now, you can read this file using File class and readAsStringSync() method.
// dart program to read from file
import 'dart:io';
void main() {
// creating file object
File file = File('test.txt');
// read file
String contents = file.readAsStringSync();
// print file
print(contents);
}
Get File Information
In this example below, you will learn how to get file information like file location, file size, and last modified time.
import 'dart:io';
void main() {
// open file
File file = File('test.txt');
// get file location
print('File path: ${file.path}');
// get absolute path
print('File absolute path: ${file.absolute.path}');
// get file size
print('File size: ${file.lengthSync()} bytes');
// get last modified time
print('Last modified: ${file.lastModifiedSync()}');
}
Note: If you try to get information of a file that does not exist, then it will throw an exception.
CSV File
A CSV (Comma Separated Values) file is a plain text file that contains data organized in a table format, where columns are separated by commas and rows are separated by line breaks. CSV files are used for:
- Data exchange between different applications.
- Data backup and restore.
- Importing and exporting data from databases.
- Automation of data processing tasks.
Read CSV File In Dart
Assume that you have a CSV file named test.csv
in the same directory of your dart program.
Name,Email,Phone
John, [email protected], 1234567890
Smith, [email protected], 0987654321
Now, you can read this file using File class and readAsStringSync() method. We will use split() method to split the string into a list of strings.
// dart program to read from csv file
import 'dart:io';
void main() {
// open file
File file = File('test.csv');
// read file
String contents = file.readAsStringSync();
// split file using new line
List<String> lines = contents.split('\n');
// print file
print('---------------------');
for (var line in lines) {
print(line);
}
}
Read Only Part Of File
You can read only part of file using substring() method. Here is an example to read only first 10 characters of file. Make sure that you have a file named test.txt in the same directory of your dart program.
Welcome to test.txt file
This is a test file.
// dart program to read from file
import 'dart:io';
void main() {
// open file
File file = new File('test.txt');
// read only first 10 characters
String contents = file.readAsStringSync().substring(0, 10);
// print file
print(contents);
}
Read File From Specific Directory
To read a file from a specific directory, you need to provide the full path of the file. Here is an example to read file from a specific directory.
// dart program to read from file
import 'dart:io';
void main() {
// open file
File file = File('C:\\Users\\test.txt');
// read file
String contents = file.readAsStringSync();
// print file
print(contents);
}
Video
Watch our video on file reading in Dart.