Search code examples
lua

Create a new file in Lua/LuaFileSystem


I've looked over the Lua & LuaFileSystem Docs and have yet to find a way to create a new file, I also scouted around on here but to the same end.

As a note, the solution I'm looking for has to be OS neutral to ensure portability, but I'm happy to get different answers for different systems.


Solution

  • Example (writing "Hello World" into test.txt):

    $ lua
    Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
    > file = io.open("test.txt", "w")
    > file:write("Hello World")
    > file:close()
    > ^D
    $ cat test.txt 
    Hello World
    

    See also: Lua IO tutorial