Belzebub

lib/file.lua

Mar 17th, 2021 (edited)
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.63 KB | None | 0 0
  1. local file = {}
  2. local _, fs = pcall(require, "filesystem")
  3. local _, json = pcall(require, "json")
  4.  
  5. function file.Write(path, content, json_encode)
  6.     local f = io.open(path, "w")
  7.     if not f then return false end
  8.  
  9.     f:write(json_encode and json.encode(content) or content)
  10.     f:close()
  11.  
  12.     return true
  13. end
  14.  
  15. function file.Read(path, json_decode)
  16.     local f = io.open(path, "r")
  17.     if not f then return false end
  18.    
  19.     local content = f:read("*a")
  20.     io.close(f)
  21.  
  22.     if json_decode then
  23.         return json.decode(content) or {}
  24.     else
  25.         return content or ""
  26.     end
  27. end
  28.  
  29. file.Exists = fs.exists
  30. file.CreateDir = fs.makeDirectory
  31.  
  32. return file
Add Comment
Please, Sign In to add comment