This chapter concludes how to read a file in dart as it is very important to not only in dart but also to every programming language.
Like as an read of a chapters in books we first need to have a book, without book we cannot read chapters from book. So To read from files in file handling we first need to have a file from which we want to read. let’s assume we have a file named book.txt in the same directory of our dart program. reading text files is versatile and vital such as reading configuration files, processing user-generated input, or loading templates for dynamic content. It has the following text before reading.
Welcome to Dart File Handling.
Now, you need to read this text in your code to read from book.txt.
import 'dart:io'; // dart program to read from file
void main(){
File re = File('book.txt');
String content = re.readAsStringSync();
print(content);
}
Here ,
The above example shows reading file from text . Now lets talk about reading a file as binary.
void main() async{
File c = File('conf.txt');
var content = await c.readASBytes();
print('The files is ${ content.lenght} bytes long.');
}
CSV ( Comma Separated Values ) files are plain text files whih have organized data flow and are easy to understand because of a table format. The columns and rows are separated by comma and line breaks respectively. ’test.csv’ is a csv file that you are going to read from. This file have following contents in it like:
First_name, Last_name
John, Doe
To read a csv file:-
import 'dart:io';
void main(){
File f = File('test.csv');
String content = f.readAsStringSync();
List<String> lines = content.splt('\n');
print('...');
for ( var line in lines ){
print(line);
}
}