---
title: "Tech Tip #22: Buffer your reads and writes (video)"
description: "Writing one line at a time means one system call per line."
url: https://greencode.ai/shorts/22-buffer-your-io/
---

# Tech Tip #22: Buffer your reads and writes (video)

## Tech Tip #22: Buffer your reads and writes

**Writing one line at a time means one system call per line.**

[Watch the video](https://media.greencode.ai/shorts/buffer-your-io-8a00f7297074.mp4)

Video created by [explainapaper.com](https://explainapaper.com/?utm_source=greencode.ai&utm_medium=film&utm_campaign=made-by)

### The problem

Unbuffered I/O turns every small read or write into its own trip to the operating system and the disk. Log files, CSV exports and socket writes are the usual culprits.

### The fix you can use today

- Wrap streams in a buffered reader or writer (BufferedWriter, io.BufferedWriter, bufio).
- Write in chunks, and stream large files instead of loading them whole.
- Flush when you need durability, not after every line.

GreenCode researchers list unbuffered reads and writes among the worst energy wasters in code.

### Tools that help

- [GreenCode Constitution skill](https://greencode-constitution.org/?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode) (GreenCode researchers): open-source energy principles and a catalogue of 37 energy anti-patterns that an AI coding agent can follow to find and fix waste like this
- [green-languages](https://github.com/PLEnergyDev/green-languages?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode) (GreenCode researchers): a command-line tool that measures the energy of whole programs or marked sections of code, across languages

### How GreenCode helps

GreenCode helps tackle this by rewriting flagged code one piece at a time. It writes any missing tests first, and returns every change for a person to approve. [More on this](https://greencode.ai/feature/automated-software-maintenance/).

### Tags

#performance#java#python

### Sources

- [GreenCode Constitution (Kapenekakis et al., GreenAI workshop, ECML PKDD 2026): unbuffered I/O among the Tier 1 anti-patterns](https://greencode.ai/publications/greencode-constitution-agent-principles-for-optimizing-code-and-llm-inference-en/)
- [GreenCode Constitution skill, energy anti-patterns: C2 unbuffered I/O: wrap byte-level streams with buffering to reduce system calls](https://greencode-constitution.org/skill.md?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode)

### Transcript

Writing one line at a time means one system call per line. Each call is a separate trip to the operating system, and often the disk. Log files, spreadsheet exports and network writes are the usual culprits. A million lines can mean a million trips. Every trip costs processor time, and that means electricity. Here is the fix you can use today. Wrap your streams in a buffered reader or writer. Then thousands of small calls become a handful of big ones. Write in chunks, and stream large files instead of loading them whole. Flush when you need the data to be safe, not after every line. GreenCode's Constitution skill can help an AI coding agent find and fix this. Then check the saving, using tools like green-languages to measure before and after. GreenCode researchers list unbuffered reads and writes among the worst energy wasters in code. GreenCode helps tackle this by rewriting flagged code one piece at a time. It writes missing tests first, and a person approves every change. That was GreenCode Tech Tip number twenty-two. Follow for the next tip, and find more on greencode.ai/tech-tips.

[The tip in full](https://greencode.ai/tech-tips/22-buffer-your-io/)

[« Tech Tip #21: Find and remove zombie resources](https://greencode.ai/shorts/21-remove-zombie-resources/)[Tech Tip #23: Put software energy in the numbers you already report »](https://greencode.ai/shorts/23-software-energy-in-your-esg-numbers/)

[All GreenCode Tech Tips](https://greencode.ai/tech-tips/) · [All GreenCode videos](https://greencode.ai/videos/)

#### Key Facts

- Tip **#22**
- For **Developers**
- Length **1:12**
- Category **Code efficiency**
![GreenCode](https://greencode.ai/wp-content/uploads/2024/12/greencode-logo-website-small-colours-2.webp)

Want this checked on your own system? Talk to the GreenCode team.

[Get in touch](https://greencode.ai/contact/)
