读取文件
将文件读取成二进制,使用read函数
use std::fs;
fn main() {
let context = fs::read("tt").unwrap();
println!("context: {:#?}", context);
}
将文件读取成字符串
use std::fs;
fn main() {
let context = fs::read_to_string("tt").unwrap();
println!("context: {}", context);
}
读取目录
use std::io;
use std::fs;
use std::path::Path;
fn visit_dirs(dir: &Path) -> io::Result {
if dir.is_dir() {
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
visit_dirs(&path)?;
} else {
let c = fs::read_to_string(path).unwrap();
println!("file = {}", c);
}
}
}
Ok(())
}
fn main() {
//let context = fs::read("tt").unwrap();
//println!("context: {:#?}", context);
//let context = fs::read_to_string("tt").unwrap();
//println!("context: {}", context);
visit_dirs(Path::new("./test")).unwrap();
}
领取专属 10元无门槛券
私享最新 技术干货