where save "write function" data on phone (android)?

Hi

I’m use this code for write time data, but i’m do not find “time.txt” on phone,where save this file(“time.txt”) ?

If this code do not  worke fore saving file(data), What functions can I use?

local path = system.pathForFile( “time.d.txt”, system.DocumentsDirectory )

local function updateTime()
    local time = os.date("*t")

    local saveData = time.hour
    local file = io.open( path, “a+” )
    file:write( saveData )
    io.close( file )

    local saveData = “:”
    local file = io.open( path, “a+” )
    file:write( saveData )
    io.close( file )

    local saveData = time.min
    local file = io.open( path, “a+” )
    file:write( saveData )
    io.close( file )

    local saveData = “:”
    local file = io.open( path, “a+” )
    file:write( saveData )
    io.close( file )

    local saveData = time.sec
    local file = io.open( path, “a+” )
    file:write( saveData )
    io.close( file )

end
local clockTimer = timer.performWithDelay( 1000, updateTime, -1 )

Help me please

Hi.  I answered a similar question in this post:

https://forums.coronalabs.com/topic/58292-ho-create-a-good-save-status-file/

In that post you can get the code to read and write files as well as an example at the end demoing how to use the code.

Whether you’ll be able to find the file on your device… I don’t know.  I just know my code works and will allow you to read and write files.

Hi

Thank for your help.

I’m used this code for write time data with your function but i can not find txt file (myTest.txt) in my device.



– function

function io.exists( fileName, base )
    local base = base or system.DocumentsDirectory    
    local fileName = fileName

    if( base ) then
        fileName = system.pathForFile( fileName, base )
    end
    if not fileName then return false end
    local f=io.open(fileName,“r”)
    if (f == nil) then
        return false
    end
    io.close(f)
    return true
end

if( io.readFile ) then
    print(“ERROR! io.readFile() exists already”)
else
    function io.readFile( fileName, base )
        local base = base or system.DocumentsDirectory
        local fileContents

        if( io.exists( fileName, base ) == false ) then
            return nil
        end

        local fileName = fileName
        if( base ) then
            fileName = system.pathForFile( fileName, base )
        end

        local f=io.open(fileName,“r”)
        if (f == nil) then
            return nil
        end

        fileContents = f:read( “*a” )

        io.close(f)

        return fileContents
    end
end

if( io.writeFile ) then
    print(“ERROR! io.writeFile() exists already”)
else
    function io.writeFile( dataToWrite, fileName, base )
        local base = base or system.DocumentsDirectory

        local fileName = fileName
        if( base ) then
            fileName = system.pathForFile( fileName, base )
        end

        local f=io.open(fileName,“a+”)
        if (f == nil) then
            return nil
        end

        f:write( dataToWrite )

        io.close(f)

    end
end

if( io.appendFile ) then
    print(“ERROR! io.appendFile() exists already”)
else
    function io.appendFile( dataToWrite, fileName, base )
        local base = base or system.DocumentsDirectory

        local fileName = fileName
        if( base ) then
            fileName = system.pathForFile( fileName, base )
        end

        local f=io.open(fileName,“a”)
        if (f == nil) then
            return nil
        end

        f:write( dataToWrite )

        io.close(f)
    end
end

if( io.readFileTable ) then
    print(“ERROR! io.readFileTable() exists already”)
else
    function io.readFileTable( fileName, base )
        local base = base or system.DocumentsDirectory
        local fileContents = {}

        if( io.exists( fileName, base ) == false ) then
            return fileContents
        end

        local fileName = fileName
        if( base ) then
            fileName = system.pathForFile( fileName, base )
        end

        local f=io.open(fileName,“r”)
        if (f == nil) then
            return fileContents
        end

        for line in f:lines() do
            fileContents[#fileContents + 1] = line
        end

        io.close( f )

        return fileContents
    end
end   

display.newText( “time”, 50, 200, native.systemFont, 16 )
local timesa= display.newText( “-”, 300, 200, native.systemFont, 16 )
local timeda= display.newText( “-”, 250, 200, native.systemFont, 16 )
local timeho= display.newText( “-”, 200, 200, native.systemFont, 16 )
local function updateTime()
    local time = os.date("*t")  
    timeho.text=time.hour
    timeda.text=time.min
    timesa.text=time.sec
    io.writeFile( time.hour, “myTest.txt” )
    io.writeFile( time.min, “myTest.txt” )
    io.writeFile( time.sec, “myTest.txt” )
end
local clockTimer = timer.performWithDelay( 1000, updateTime, -1 )

  1. Can’t help you with finding files on your device.  I assume you mean you want to plug the device into your PC and find it that way?  I’m going to assume it will be hidden from you, but I never do this, so… don’t know.

  2. Please format your code with code blocks:

formatyourcode.jpg

This is bad:

local function doit()

                               print(“Some hard to read code”)

          end

This is good (and will get more answers):

local function doit() print("Some properly formatted and displayed code") end

Hi.  I answered a similar question in this post:

https://forums.coronalabs.com/topic/58292-ho-create-a-good-save-status-file/

In that post you can get the code to read and write files as well as an example at the end demoing how to use the code.

Whether you’ll be able to find the file on your device… I don’t know.  I just know my code works and will allow you to read and write files.

Hi

Thank for your help.

I’m used this code for write time data with your function but i can not find txt file (myTest.txt) in my device.



– function

function io.exists( fileName, base )
    local base = base or system.DocumentsDirectory    
    local fileName = fileName

    if( base ) then
        fileName = system.pathForFile( fileName, base )
    end
    if not fileName then return false end
    local f=io.open(fileName,“r”)
    if (f == nil) then
        return false
    end
    io.close(f)
    return true
end

if( io.readFile ) then
    print(“ERROR! io.readFile() exists already”)
else
    function io.readFile( fileName, base )
        local base = base or system.DocumentsDirectory
        local fileContents

        if( io.exists( fileName, base ) == false ) then
            return nil
        end

        local fileName = fileName
        if( base ) then
            fileName = system.pathForFile( fileName, base )
        end

        local f=io.open(fileName,“r”)
        if (f == nil) then
            return nil
        end

        fileContents = f:read( “*a” )

        io.close(f)

        return fileContents
    end
end

if( io.writeFile ) then
    print(“ERROR! io.writeFile() exists already”)
else
    function io.writeFile( dataToWrite, fileName, base )
        local base = base or system.DocumentsDirectory

        local fileName = fileName
        if( base ) then
            fileName = system.pathForFile( fileName, base )
        end

        local f=io.open(fileName,“a+”)
        if (f == nil) then
            return nil
        end

        f:write( dataToWrite )

        io.close(f)

    end
end

if( io.appendFile ) then
    print(“ERROR! io.appendFile() exists already”)
else
    function io.appendFile( dataToWrite, fileName, base )
        local base = base or system.DocumentsDirectory

        local fileName = fileName
        if( base ) then
            fileName = system.pathForFile( fileName, base )
        end

        local f=io.open(fileName,“a”)
        if (f == nil) then
            return nil
        end

        f:write( dataToWrite )

        io.close(f)
    end
end

if( io.readFileTable ) then
    print(“ERROR! io.readFileTable() exists already”)
else
    function io.readFileTable( fileName, base )
        local base = base or system.DocumentsDirectory
        local fileContents = {}

        if( io.exists( fileName, base ) == false ) then
            return fileContents
        end

        local fileName = fileName
        if( base ) then
            fileName = system.pathForFile( fileName, base )
        end

        local f=io.open(fileName,“r”)
        if (f == nil) then
            return fileContents
        end

        for line in f:lines() do
            fileContents[#fileContents + 1] = line
        end

        io.close( f )

        return fileContents
    end
end   

display.newText( “time”, 50, 200, native.systemFont, 16 )
local timesa= display.newText( “-”, 300, 200, native.systemFont, 16 )
local timeda= display.newText( “-”, 250, 200, native.systemFont, 16 )
local timeho= display.newText( “-”, 200, 200, native.systemFont, 16 )
local function updateTime()
    local time = os.date("*t")  
    timeho.text=time.hour
    timeda.text=time.min
    timesa.text=time.sec
    io.writeFile( time.hour, “myTest.txt” )
    io.writeFile( time.min, “myTest.txt” )
    io.writeFile( time.sec, “myTest.txt” )
end
local clockTimer = timer.performWithDelay( 1000, updateTime, -1 )

  1. Can’t help you with finding files on your device.  I assume you mean you want to plug the device into your PC and find it that way?  I’m going to assume it will be hidden from you, but I never do this, so… don’t know.

  2. Please format your code with code blocks:

formatyourcode.jpg

This is bad:

local function doit()

                               print(“Some hard to read code”)

          end

This is good (and will get more answers):

local function doit() print("Some properly formatted and displayed code") end