Jump to content

How to read a txt file in script


DarkingCheater

Recommended Posts

Posted

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"

Posted

All devices not truth road exam.. /storage/sdcard/Notes or /storage/Mfc041/Notes .. u understand ?

Posted
2 minutes ago, goldenkaramel said:

All devices not truth road exam.. /storage/sdcard/Notes or /storage/Mfc041/Notes .. u understand ?

it was just an example you choose the directory

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.