突然想到要一个对大数据大文件的存储 写了个数据存储方式 以及对文件的写入 个人觉得这些还是有些用处
%%对路径处理模块查询路径中的模块
%%以及数据的读写
-module(server_file_util).
-export([
% get_server_dir/0,
set_dirs/1,
set_dirs/2,
set_user_dirs/1,
set_dir_by_path/1,
judge_dir_or_set/1,
read_file/1,
read_file/2,
write_file/4,
set_dir_day/1,
set_dir_by_path/2
]).
%file:get_cwd(), %%获取当前目录
%%创建每日目录
set_dir_day(Pwd) when is_list(Pwd) -> %%
{{Year, Month, Day},{_Hour, _Minute, _Second}} = calendar:local_time(), %%{{2013,5,17},{16,37,30}}
YearList =integer_to_list(Year),
MonthList =integer_to_list(Month),
DayList =integer_to_list(Day),%%file:make_dir("/mnt/hgfs/G/testyourself/server/abc").
Daydir = Pwd ++ "/" ++ YearList ++ "/" ++ MonthList ++ "/" ++ DayList,
judge_dir_or_set(Daydir),
Daydir.
%%判断目录是否存在并进行新建目录
judge_dir_or_set(Path) ->
case filelib:is_dir(Path) of
true ->
ok;
false ->
set_dir_by_path(Path)
end.
set_dir_by_path(Path) ->
PathList = string:tokens(Path,"/"),
set_dir_by_path(PathList,"").
set_dir_by_path([],Dir) -> Dir;
set_dir_by_path([Path|PathList],Dir) ->
DirList = filename:join(Dir, Path),
case filelib:is_dir(DirList) of
true ->
set_dir_by_path(PathList,DirList);
false ->
file:make_dir(DirList),
set_dir_by_path(PathList,DirList)
end.
%%生成多个子目录接口
%%生成用户目录极其子目录
set_user_dirs(User) ->
UserPath = case catch(ets:lookup(user_data_dir,day_path)) of
[{day_path, DayPath}] ->
judge_dir_or_set(DayPath),
set_dir_by_path([User],DayPath);
_ ->
error
end,
UserDirList = case catch(ets:lookup(user_data_dir,default_dir)) of
[{default_dir, DirList}] ->
DirList;
_ ->
error
end,
set_dirs(UserPath,UserDirList).
%%默认生成多个子目录接口
set_dirs(Path) ->
set_dirs(Path,"").
%%自定义生成多个子目录接口
set_dirs(Path,DirList) ->
lists:foreach(fun(X) ->
judge_dir_or_set(Path),
set_dir_by_path([X],Path)
end,DirList).
%%在目录下面存文件
write_file(Path,Bin,FileType,FileName) ->
Extension = case FileType of
_ -> ".txt"
end,
FullName = filename:join(Path, FileName ++ Extension),
file:write_file(FullName,Bin).
%%读取文件
read_file(FullName) ->
Bin = case file:read_file(FullName) of
{ok, Binary} ->
Binary;
{error,enoent} ->
error;
_->
error
end,
Bin.
read_file(Path,FileName) ->
FullName = filename:join(Path,FileName),
read_file(FullName).
%%删除文件