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