loadscript filetest
-- Script to write two lines to a file.
-- Append two lines to the same file.
-- Read the entire file contents and print them.
-- Open the file for writing.
myfile = io.open("/usb1/myfiletest", "w")
if io.type(myfile) == "file" then
myfile:write("This is my first line WRITING\n")
myfile:write("This is my next line WRITING\n")
myfile:close()
-- Open the file for appending.
myfile = io.open("/usb1/myfiletest", "a")
if io.type(myfile) == "file" then
myfile:write("This is my first APPEND line\n")
myfile:write("This is my next APPEND line\n")
myfile:close()
-- Open the file for reading.
myfile = io.open("/usb1/myfiletest", "r")
if io.type(myfile) == "file" then
filecontents = myfile:read("*a")
print("the file contains:")
print()
print(filecontents)
myfile:close()
else
print("The file did not open correctly for reading")
end
else
print("The file did not open correctly for appending")
end
else
print("The file did not open correctly for writing")
end
endscript
After downloading the above script, type filetest() to execute the script. Here are the output
results:
the file contains:
This is my first line WRITING
This is my next line WRITING
This is my first APPEND line
This is my next APPEND line