From ed4cc33e11198af0197e5a1d9529373ecb5a86c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=81=92?= Date: Tue, 14 May 2024 15:17:06 +0800 Subject: [PATCH] Add warning prompt for File write operations (#13557) --- .../pages/getting-started/io-and-the-file-system.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/elixir/pages/getting-started/io-and-the-file-system.md b/lib/elixir/pages/getting-started/io-and-the-file-system.md index 57a4760633e..50c9b333698 100644 --- a/lib/elixir/pages/getting-started/io-and-the-file-system.md +++ b/lib/elixir/pages/getting-started/io-and-the-file-system.md @@ -27,6 +27,10 @@ hello world The `File` module contains functions that allow us to open files as IO devices. By default, files are opened in binary mode, which requires developers to use the specific `IO.binread/2` and `IO.binwrite/2` functions from the `IO` module: +> #### Potential data loss warning {: .warning} +> +> The following code opens a file for writing. If an existing file is available at the given path, its contents will be deleted. + ```elixir iex> {:ok, file} = File.open("path/to/file/hello", [:write]) {:ok, #PID<0.47.0>} @@ -38,7 +42,7 @@ iex> File.read("path/to/file/hello") {:ok, "world"} ``` -A file can also be opened with `:utf8` encoding, which tells the `File` module to interpret the bytes read from the file as UTF-8-encoded bytes. +The file could be opened with the `:append` option, instead of `:write`, to preserve its contents. You may also pass the `:utf8` option, which tells the `File` module to interpret the bytes read from the file as UTF-8-encoded bytes. Besides functions for opening, reading and writing files, the `File` module has many functions to work with the file system. Those functions are named after their UNIX equivalents. For example, `File.rm/1` can be used to remove files, `File.mkdir/1` to create directories, `File.mkdir_p/1` to create directories and all their parent chain. There are even `File.cp_r/2` and `File.rm_rf/1` to respectively copy and remove files and directories recursively (i.e., copying and removing the contents of the directories too). @@ -96,7 +100,7 @@ With this, we have covered the main modules that Elixir provides for dealing wit You may have noticed that `File.open/2` returns a tuple like `{:ok, pid}`: ```elixir -iex> {:ok, file} = File.open("hello", [:write]) +iex> {:ok, file} = File.open("hello") {:ok, #PID<0.47.0>} ```