Jump to content

How to read a txt file in script


DarkingCheater

Recommended Posts

Well, to read a txt file you need the directory and filename.

Below you will have a function where it will open the file and read it and return the text of it.

local open = io.open 
local function read_file(path) 
local file = open(path, "rb")
 if not file then return nil end 
local content = file:read 
"*a"
file:close()
return content
 end 

Example

in the txt file that is located in the android Notes folder is stored in the text "TestRead"

local open = io.open 
local function read_file(path) 
local file = open(path, "rb")
 if not file then return nil end 
local content = file:read 
"*a"
file:close()
return content
 end 

Text  = read_file('/storage/sdcard0/Notes/File.txt')

if Text == nil then
print('The directory or file name is wrong')
else
print(Text)
end

will return "TestRead"

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.