隱私權政策

搜尋此網誌

技術提供:Blogger.

關於我自己

我的相片
目前從事軟體相關行業,喜歡閱讀、健身、喝調酒。習慣把遇到的問題記下來,每天做一些整理方便自己以後查。 Python、Rust、Kotlin等程式語言皆為自學,目前比較著重在Rust語言,歡迎一起討論。

2023年12月26日 星期二

Rust OpenOptions概述 - 文件打開的權限


Struct std::fs::OpenOptions[1]

OpenOptions主要是可以修改文件打開的權限

例如讀取(read)、撰寫(write)、創建(create)等等

沒有開啟權限會無法做相對應的事

本文介紹new、open和六種權限

權限分別是

讀取(read)

撰寫(write)

追加(append)

創建(create)

創建新的(create_new)

截斷(truncate)

new[2]

pub fn new() -> Self

new為創建新的空白選項

所有預設值為false


read[3]

設定檔案內容可以被讀取

可以以read_to_string等方式讀取檔案內容

假設有一個example.txt,內容為abcdefghijklmnopqrstuvwxyz


程式例子

use std::{fs::OpenOptions, io::Read};

fn main() {
    let file_path = "example.txt";
    let mut file = OpenOptions::new().read(true).open(file_path).unwrap();
    let mut buf = String::new();
    file.read_to_string(&mut buf).unwrap();
    println!("buf: {}", buf);
}
buf: abcdefghijklmnopqrstuvwxyz


write[4]

設定檔案可以被寫入

use std::{fs::OpenOptions, io::Write};

fn main() {
    let file_path = "example.txt";
    let mut file = OpenOptions::new().write(true).open(file_path).unwrap();
    let buf = String::from("1234567890");
    file.write_all(buf.as_bytes()).unwrap();
}

要小心write_all為直接覆蓋

最後會長



append[5]

append為在內容後方新增資料

當.write(true).append(true)和只有append(true)效果是一樣的

use std::{fs::OpenOptions, io::Write};

fn main() {
    let file_path = "example.txt";
    let mut file = OpenOptions::new().append(true).open(file_path).unwrap();
    let buf = String::from("1234567890");
    file.write_all(buf.as_bytes()).unwrap();
}



create[6]

當檔案不存在時create可以創建新的檔案

當檔案存在時會自動開啟

使用create要和write或append也必須要啟用

use std::fs::OpenOptions;

fn main() {
    let file_path = "example.txt";
    let file = OpenOptions::new()
        .write(true)
        .create(true)
        .open(file_path)
        .unwrap();
}


create_new[7]

創建一個新的文件

如果該文件已經存在會fail

一樣write或append必須啟用才可以使用create_new

而create_new啟用時,create和truncate會被忽略

use std::fs::OpenOptions;

fn main() {
    let file_path = "example.txt";
    let file = OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(file_path)
        .unwrap();
}


truncate[8]

truncate為截斷

如果文件已存在且裡面有內容

會將長度截斷成0

意思就是會把裡面內容全部刪掉

因為會修改到裡面內容

write也必須是啟用的

use std::fs::OpenOptions;

fn main() {
    let file_path = "example.txt";
    let file = OpenOptions::new()
        .write(true)
        .truncate(true)
        .open(file_path)
        .unwrap();
}



open[9]

pub fn open<P: AsRef<Path>>(&self, path: P) -> Result<File>

最後則是open

open為輸入檔案路徑

輸出Result,裡面內容為File

File的設定跟上述的六個有關

而有幾種狀況會Err


參考資料

[1] https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#

[2] https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.new

[3] https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.read

[4] https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.write

[5] https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.append

[6] https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.create

[7] https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.create_new

[8] https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.truncate

[9] https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.open

0 comments:

張貼留言