erlang生成多个子目录存文件读文件

突然想到要一个对大数据大文件的存储 写了个数据存储方式 以及对文件的写入 个人觉得这些还是有些用处

%%对路径处理模块查询路径中的模块
%%以及数据的读写

-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).

%%删除文件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值