0Pricing
Dart Academy · Lesson

Reading and Writing Files

Load and save text and bytes.

Reading and Writing Files is a free Dart Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why dart:io

To touch real files from a command-line program, you import dart:io. It gives Dart the power to read and write your disk.

import 'dart:io';

Meet the File Class

A File object points at a path on disk. Creating one does not open or read anything yet, it just names the target.

final file = File('notes.txt');

Read a Whole File

Use readAsString to pull an entire text file into one String. It returns a Future, so you await the result.

final text = await file.readAsString();

Read Line by Line

Prefer a list of lines? readAsLines hands you each row as its own String, perfect for processing rows one at a time.

final lines = await file.readAsLines();

Write Text Out

To save text, call writeAsString. It creates the file if missing and replaces any existing content by default.

await file.writeAsString('Hello, disk!');

Append Instead of Replace

Want to keep the old text and add more? Pass FileMode.append so your new content lands at the end.

await file.writeAsString('line\n', mode: FileMode.append);

Working With Bytes

Files are not always text. Use readAsBytes to get raw data as a list of integers for images or binary formats.

final bytes = await file.readAsBytes();

Check It Exists First

Reading a missing file throws. Guard yourself by asking exists before you touch the file at all.

if (await file.exists()) { /* safe to read */ }

Sync Versions

Every async method has a blocking twin like readAsStringSync. Handy in tiny scripts, but it freezes everything while it runs.

final text = file.readAsStringSync();

Delete and Rename

Clean up with delete, or move a file by calling rename with a new path. Both return Futures you can await.

await file.rename('archive.txt');

Pick the Right Encoding

Text methods default to utf8. For other encodings, pass an explicit encoding so accented or special characters survive the round trip.

await file.writeAsString(s, encoding: latin1);

Quick Check

You want to add a log line without erasing existing content. Which option does that?

Recap

You can now read files as String, lines, or bytes, write or append text, and guard with exists. That is the core of file I/O.

Frequently asked questions

Is the “Reading and Writing Files” lesson free?

Yes — the full text of “Reading and Writing Files” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.

What will I learn in “Reading and Writing Files”?

Load and save text and bytes. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Dart Academy?

No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Reading and Writing Files” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Dart Academy lesson?

Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Reading and Writing Files
  2. Directories, Paths, and FileSystemEntity
  3. Streaming Large Files
  4. Stdin, Stdout, and Process Args
← Back to Dart Academy