Dart Starting to In-depth Roadmap:
Dart Language — 45 minutes Quick Start (PART 1)
Dart Language — 45 minutes Quick Start (part 2)
Dart asynchronous programming in detail
Dart language asynchronous Stream details
Dart language standard flow and file manipulation
Dart Network programming
Dart crawler development experience
Dart full stack server development
Dart FFI calls C hybrid programming
Dart and Lua invocation in LuaDardo
Pay attention to me, not bad!
Standard input output stream
stdin
stdout
stderr
// Import the IO package
import 'dart:io';
void main() {
// Write a string to the standard output stream
stdout.write('root\$:');
// Reads a string from the standard input stream
var input = stdin.readLineSync();
// Write data with newline character
stdout.writeln("input data:$input");
// Write data to the standard error stream
stderr.writeln("has not error");
}
Copy the code
In addition to readLineSync, stdin can use readByteSync to read a single byte.
File operations
Write files
A simple operation mode, no need to manually close the file, the file will be automatically closed after the completion of writing
import 'dart:io';
void main() async{
// Create a file
File file = new File('test.txt');
String content = 'The easiest way to write text to a file is to create a File';
try {
// Writes a string to the file
await file.writeAsString(content);
print('Data written.');
} catch (e) {
print(e); }}Copy the code
WriteAsString prototype
Future<File> writeAsString(String contents,
{FileMode mode: FileMode.write,
Encoding encoding: utf8,
bool flush: false})
Copy the code
mode
File mode, which Rimmer thinks is write modeencoding
Character encoding. The default is UTF-8flush
Whether to flush the cache immediately. Default is false
Constant for FileMode FileMode
Constant values | instructions |
---|---|
read | Read-only mode |
write | Readable and writable mode, overwriting if the file exists |
append | Append mode, readable and writable, appends to the end if the file exists |
writeOnly | Only write mode |
writeOnlyAppend | Append mode in write mode only, not read |
In addition to the writeAsString method, you can use writeAsBytes to write a list of bytes. Note that both of these methods are executed asynchronously, returning a Future, and you can also use synchronous methods to perform writes if necessary
writeAsStringSync
writeAsBytesSync
For more flexible control, you can use the following method to operate the file, but you need to manually close the file
import 'dart:io';
void main() async{
// Create a file
File file = new File('test.txt');
// Set file mode to append
IOSink isk = file.openWrite(mode: FileMode.append);
// Write multiple times
isk.write('A woman is like a tea bag');
isk.writeln('you never know how strong it is until it\'s in hot water.');
isk.writeln('-Eleanor Roosevelt');
await isk.close();
print('Done! ');
}
Copy the code
Read the file
Easy way
readAsBytes
readAsBytesSync
readAsString
readAsStringSync
readAsLines
readAsLinesSync
void main() async{
File file = new File('test.txt');
try{
String content = await file.readAsString();
print(content);
}catch(e){
print(e); }}Copy the code
Another, lower level approach
import 'dart:io';
import 'dart:convert';
void main() async{
try {
// LineSplitter is a line break encapsulated in the Dart language, where text is split by line
Stream lines = new File('test.txt').openRead()
.transform(utf8.decoder).transform(const LineSplitter());
await for (var line in lines) {
print(line); }}catch(_) {}}Copy the code
Other operations on files
import 'dart:io';
void main() async{
File file = new File('test.txt');
// Check whether the file exists
if(await file.exists()){
print("File exists");
}else{
print("File does not exist");
}
// Copy the file
await file.copy("test-1.txt");
// Change the file name. Can be used to move files when different paths are passed in
await file.rename("test-2.txt");
// Get the file size
print(await file.length());
}
Copy the code
In turn, these methods have synchronized versions with the Sync suffix, such as copySync, renameSync, and so on.
To get more information about a File, you can also use FileSystemEntity, a superclass of multiple classes such as File
import 'dart:io';
void main() async{
String path = 'test.txt';
// Check whether the path is a folder
if (!await FileSystemEntity.isDirectory(path)) {
print('$path is not a directory');
}
Directory dir = Directory(r'D:\workspace\dart_space\Tutorial');
// Whether the directory exists
if(await dir.exists()){
// Get FileSystemEntity objects from the list method of the directory
Stream<FileSystemEntity> fse = await dir.list();
await for (FileSystemEntity entity in fse) {
if(entity is File){
print("entity is file");
}
// Prints the file information
print(await entity.stat());
/ / delete
awaitentity.delete(); }}else{
// Create one if it does not exist. When recursive is true, create all nonexistent directories on the path
await dir.create(recursive: true); }}Copy the code
Note that delete contains an optional parameter, prototype Future
delete({bool recursive: false}). Recursive defaults to false. When deleting a directory, the directory must be empty before deleting it. When recursive is set to true, all subdirectories and files under the directory are deleted.
Video course
Related video courses posted by bloggers
Dart Programming Guide for Flutter full stack development
Guide to the full stack development of Flutter