상세 컨텐츠

본문 제목

Dart - File Handling

Dart

by techbard 2024. 1. 5. 20:41

본문

반응형
import 'dart:io';

void main(List<String> args) {
  // Open file
  Directory tempDir = Directory.systemTemp;
  File file = File('${tempDir.path}/test.txt');

  // Read File In Dart
  String text = file.readAsStringSync();
  print(text);

  // Get File Information
  print('File path: ${file.path}');
  print('File absolute path: ${file.absolute.path}');
  print('File size: ${file.lengthSync()} bytes');
  print('Last modified: ${file.lastModifiedSync()}');
}

// 결과
Welcome to test.txt file.
This is a test file.
File path: C:\Users\ADMINI~1\AppData\Local\Temp/test.txt
File absolute path: C:\Users\ADMINI~1\AppData\Local\Temp/test.txt
File size: 47 bytes
Last modified: 2024-01-05 22:17:31.000

 

import 'dart:io';

void main(List<String> args) {
  // Read CSV File In Dart
  Directory tempDir = Directory.systemTemp;
  File file = File('${tempDir.path}\\test.csv');
  String contents = file.readAsStringSync();
  List lines = contents.split('\n');

  print('---------------------');

  for (var line in lines) {
    print(line);
  }

  // Read Only Part Of File
  String contentsPart = file.readAsStringSync().substring(0, 10);
  print(contentsPart);
}

// 결과
---------------------
Name,Email,Phone

John, john@gmail.com, 1234567890

Smith, smith@gmail.com, 0987654321
Name,Email

 

import 'dart:io';

void main(List<String> args) {
// Write File In Dart
  Directory tempDir = Directory.systemTemp;
  File file = File("${tempDir.path}\\text2.txt");
  file.writeAsStringSync('Welcome to test.txt files.');
  print('File written.');
  file.writeAsStringSync('\nThis is a new content.', mode: FileMode.append);
  print('Congrat!! New content is added on top od previous content.');

  print('---------------------');

  File file2 = File("${tempDir.path}\\text2.txt");
  file2.deleteSync();
  print('File deleted.');
}

// 결과
File written.
Congrat!! New content is added on top od previous content.
---------------------
File deleted.

 

import 'dart:io';

void main(List<String> args) {
  Directory tempDir = Directory.systemTemp;
  File file = File('${tempDir.path}\\_deletabletest.txt');

  // file.writeAsStringSync('Welcome to test.txt files.');

  if (file.existsSync()) {
    file.deleteSync();
    print('File deleted.');
  } else {
    print('File does not exist.');
  }
}

// 결과
File does not exist.

File deleted.

 

import 'dart:io';

void main(List<String> args) {
  print('Please enter a file name to read from:');
  var fileName = stdin.readLineSync();

  double sum = 0.0;
  var students = [];

  final lines = File(fileName!).readAsLinesSync();
  lines.removeAt(0); // remove header row
  for (var line in lines) {
    final values = line.split(',');
    sum += double.parse(values[2]);

    students.add({
      'firstName': values[0],
      'lastName': values[1],
      'marks': values[2],
    });
  }

  print('The avg of all marks is ${(sum / lines.length).toStringAsFixed(2)}');
  print('\n\n');
  print(students);
}

// 결과
Please enter a file name to read from:
marks.csv
The avg of all marks is 74.56

// marks.csv

FirstName,LastName,Mark
John,Rambo,100
Peter,Pollock,80
Kevin,Hart,95
Morgan,Freeman,97
Tony,Stark,70
Jessica,Pollard,64
Mike,McDonald,44
James,Small,33
James,Mogoreng,88
반응형

관련글 더보기

댓글 영역