file_operation/write/sync/
fn.rs1use crate::*;
2
3pub fn write_to_file(file_path: &str, content: &[u8]) -> Result<(), Error> {
14 if let Some(parent_dir) = std::path::Path::new(file_path).parent() {
15 std::fs::create_dir_all(parent_dir)?;
16 }
17 std::fs::OpenOptions::new()
18 .write(true)
19 .create(true)
20 .truncate(true)
21 .open(file_path)
22 .and_then(|mut file| std::io::Write::write_all(&mut file, content))
23}
24
25pub fn append_to_file(file_path: &str, content: &[u8]) -> Result<(), Error> {
36 if let Some(parent_dir) = std::path::Path::new(file_path).parent() {
37 std::fs::create_dir_all(parent_dir)?;
38 }
39 std::fs::OpenOptions::new()
40 .create(true)
41 .append(true)
42 .open(file_path)
43 .and_then(|mut file| std::io::Write::write_all(&mut file, content))
44}