Read a text file
Save the sample file beside the program. Read the whole file and print the line it contains.
File.readAsStringSync
Simple example
Read the word saved in the file.
Startinput.txt
Resulthello
hello
import 'dart:io';
void main() {
final text = File("input.txt").readAsStringSync();
print(text.trimRight());
}
hello
Advertisements
Everyday examples
Read the pickup code that the library saved for the next shift.
Startinput.txt
ResultB12
B12
import 'dart:io';
void main() {
final text = File("input.txt").readAsStringSync();
print(text.trimRight());
}
B12
Read the room number that the teacher saved for the morning class.
Startinput.txt
ResultRoom 8
Room 8
import 'dart:io';
void main() {
final text = File("input.txt").readAsStringSync();
print(text.trimRight());
}
Room 8
Show the next stop stored in the bus display file.
Startinput.txt
ResultOak Street
Oak Street
import 'dart:io';
void main() {
final text = File("input.txt").readAsStringSync();
print(text.trimRight());
}
Oak Street