引用
erlang 生成dets成功事例
-module (dets_upload).
-compile(export_all).
-export([benchmark/1]).
benchmark(Ticket) ->
{ok,ListAll} = file:read_file("d:/erlangdemo/dets/"++Ticket++".txt"),
ListAll1 = binary_to_list(ListAll),
dets:open_file(Ticket, [{type, set},{file,"D:/erlangdemo/dets/"++Ticket++""}]),
Array = parse_to_line_array(ListAll1,["\r","\n"]),
loopList(Array,Ticket),
io:format("Upload Success!").
%循环将数据插入DETS
loopList([H|T],Ticket) ->
Line = list_to_term(H),
dets:insert(Ticket,Line),
loopList(T,Ticket);
loopList([],Ticket) -> ok.
%解析数据
parse_to_line_array(String, []) ->
io:format("attention!!! parse line error !!!!!!!!!! ~p~n", [String]), [];
parse_to_line_array(String, [H|T]) ->
Array = string:tokens(String, H),
case length(Array) of
1 -> parse_to_line_array(String, T);
LineNo when LineNo > 1 -> Array
end.
list_to_term(String) ->
{ok, T, _} = erl_scan:string(String++"."),
case erl_parse:parse_term(T) of
{ok, Term} ->
Term;
{error, Error} ->
Error
end.
.txt文件下面有可以下载
在本地建立文件夹 放里面就可以用了
祝你好运!