# Console, files, JSON, and environment

> Read and write console data and files, convert JSON, and access environment values.

Canonical: https://alice-lang.org/documentation/library/io/

[Documentation index](https://alice-lang.org/documentation/llms.txt) · [Complete book](https://alice-lang.org/llms-full.txt)

## Console functions

| Function       | Behavior                                               |
| -------------- | ------------------------------------------------------ |
| `puts(value)`  | Print a value and a newline                            |
| `print(value)` | Print without appending a newline                      |
| `p(value)`     | Print an inspected representation and return the value |
| `gets`         | Read a line; may return nil at end of input            |
| `exit(code)`   | End the process with an exit code                      |

```alice
print "Name: "
name = gets
if name.nil?
  puts "No input"
else
  puts "Hello, #{name}!"
end
```

Interactive console input is a local CLI workflow. The browser playground does not provide the same stdin or operating-system environment as a terminal.

## Files

```alice
write_file("greeting.txt", "Hello, Alice!\n")
puts read_file("greeting.txt")
puts file_exists?("greeting.txt")
```

`read_file` reads text, `write_file` writes text, `file_exists?` checks existence, and `read_lines` reads lines. Relative paths are relative to the process's working directory. A failed open or write raises an error; use [rescue](https://alice-lang.org/documentation/language/exceptions.md) for failures you can recover from.

For directories, streams, encodings, and path construction, use `System.IO`. Use `Path.Combine` instead of hard-coding platform separators. Dispose streams when their work is complete.

## JSON

`to_json(value)` converts primitives, strings, dictionaries, and enumerable collections to JSON. Other object types fall back to their textual representation; it is not an automatic record-property serializer. `parse_json(text)` returns a .NET `JsonNode` and rejects a JSON-null document.

```alice
puts to_json({"message" => "Hello, Alice!"})
```

For typed record/class serialization, use `System.Text.Json.JsonSerializer` with the appropriate .NET serializer configuration. The [web guide](https://alice-lang.org/documentation/guides/web.md) uses ASP.NET Core's JSON response machinery. Validate parsed input before using it in calculations or decisions.

## Environment and time

`env(name)` reads an environment value; absent values need handling. `now` supplies the current time and `sleep(seconds)` pauses synchronously. Use .NET APIs when you need explicit time zones, monotonic timing, or cancellation.

Do not block a worker thread with `sleep` for async waiting. Use `await Task.Delay(...)` in an [async method](https://alice-lang.org/documentation/concurrency/tasks.md).

## Execution environments

Files, network access, native libraries, and framework packages depend on the host. Browser examples focus on portable language behavior. Run file, server, desktop, and NuGet examples locally with the documented project dependencies.

## Continue reading

- Previous: [Regular expressions](https://alice-lang.org/documentation/library/regex.md)
- Next: [SIMD, parallel, and sparse math](https://alice-lang.org/documentation/library/numerics.md)
