file_operation/write/async/
fn.rs1use tokio::fs::{OpenOptions, create_dir_all};
2use tokio::io::{AsyncWriteExt, Error};
3
4pub async fn async_write_to_file(file_path: &str, content: &[u8]) -> Result<(), Error> {
15 if let Some(parent_dir) = std::path::Path::new(file_path).parent() {
16 create_dir_all(parent_dir).await?;
17 }
18 let mut file = OpenOptions::new()
19 .write(true)
20 .create(true)
21 .truncate(true)
22 .open(file_path)
23 .await?;
24 file.write_all(content).await?;
25 Ok(())
26}
27
28pub async fn async_append_to_file(file_path: &str, content: &[u8]) -> Result<(), Error> {
39 if let Some(parent_dir) = std::path::Path::new(file_path).parent() {
40 create_dir_all(parent_dir).await?;
41 }
42 let mut file = OpenOptions::new()
43 .write(true)
44 .create(true)
45 .append(true)
46 .open(file_path)
47 .await?;
48 file.write_all(content).await?;
49 Ok(())
50}