io.input()

Type Function
Library io.*
Return value Object
Revision Release 2024.3703
Keywords io, input, file
See also io.output()
io.read()

Overview

Sets the standard input file. When called with a file handle, it simply sets this file handle as the default input file. When called without parameters, it returns the current default input file.

This function returns a file handle of the opened file or the handle to the current input file.

Syntax

io.input( file )
file (optional)

Object. File to set as the standard input file. You must use system.pathForFile() to create a file name and path to the system.ResourceDirectory, system.DocumentsDirectory, system.ApplicationSupportDirectory, system.TemporaryDirectory, or system.CachesDirectory constants.

Example

local path = system.pathForFile( "data.txt", system.DocumentsDirectory )

local tmp = io.input()  -- Save current file handle
io.input( path )        -- Open new file in text mode

print( io.read() )      -- Read and display the file

io.input():close()      -- Close the file
io.input( tmp )         -- Restore the previous file