Generates an absolute path using system-defined directories as the base (see System-defined directories below). A second, optional parameter baseDirectory specifies which base directory is used to construct the full path; its default value is system.ResourceDirectory.
If the base directory is system.ResourceDirectory and the generated path points to a non-existent file, "nil" is returned and a warning message is displayed in the Corona terminal.
Related Items:
system.ResourceDirectory -- returns constant to the App Bundle folder
system.DocumentsDirectory -- returns constant to /Documents folder
system.TemporaryDirectory -- returns constant to /tmp folder
1 | system.pathForFile( filename [, baseDirectory] ) |
local path = system.pathForFile( "data.txt", system.DocumentsDirectory ) local fhd = io.open( path ) -- Determine if file exists if fhd then print( "File exists" ) fhd.close() else print( "File does not exist!" ) end
filename
File name string.
baseDirectory
Directory where the file is located. Assumes ResourceDirectory if this parameter is missing.
String -- absolute path to file.
Note: On Android devices this API doesn't currently work on system.ResourceDirectory because there is no Resource folder. It's basically a zip file holding the resource files.
-The above example will produce a Runtime Error (see below):
Runtime error
/Users/paulh/Desktop/system.pathForFile/main.lua:7: bad argument #1 to 'close' (FILE* expected, got nil)
stack traceback:
[C]: ?
[C]: in function 'close'
/Users/paulh/Desktop/system.pathForFile/main.lua:7: in main chunk
Runtime error: /Users/paulh/Desktop/system.pathForFile/main.lua:7: bad argument #1 to 'close' (FILE* expected, got nil)
stack traceback:
[C]: ?
[C]: in function 'close'
/Users/paulh/Desktop/system.pathForFile/main.lua:7: in main chunk
-Solution
Replace fhd.close() with io.close( fhd ) (see code below)
1 2 3 4 5 6 7 8 9 10 11 | local path = system.pathForFile( "data.txt", system.DocumentsDirectory ) local fhd = io.open( path ) -- Determine if file exists if fhd then print( "File exists" ) --fhd.close() <- Produces a Runtime Error. Use io.close( fhd ) io.close( fhd ) else print( "File does not exist!" ) end |
Sincerely,
Paul Allan Harrington
http://www.pahgroup.com
Graham Ranson over at Lime (http://justaddli.me/blog.php) has suggested that:
1 | fhd.close( fhd ) |
should work as well and it does. Thanks Graham.
Best wishes,
Paul Allan Harrington
http://www.pahgroup.com
Shouldn't you check for path too? Because if I don't I get a Runtime error from the open function if the string given to system.pathForFile doesn't map to an actual file.
Runtime error
... bad argument #1 to 'open' (string expected, got nil)
1 2 3 4 5 6 7 8 9 10 11 12 | local path = system.pathForFile( "data.txt", system.ResourceDirectory ) if path then -- file exists local file = io.open( path, "r" ) if file then -- nil if no file found local contents = file:read( "*a" ) print( "Contents of " .. path .. "\n" .. contents ) io.close( file ) end else print( 'file does not exist' ) end |
The file must exist when you specify the Resource Directory or a runtime warning will occur and path will be nil. Since this directory is read-only, your program should know what files are there.
You could check to make sure the value of path is not nil but that would mean you didn't include the file in your build.
Thanks,
Tom
While checking for nil might seem useless because "you should know whether the file is there" it comes in handy for me during development. I have a button routine that puts up a button and shows a depressed version when pressed...
...except when I'm developing and haven't done the depressed version yet, it will still work with just the one button...
...and even before that if there's NO button artwork yet it will display a text object that acts like a button.
Which means I can use the same button code from day 1 to the day I ship and never have to change anything except throw the button artwork in whenever I get it. :)
Just one example of how this can be used.
Jay
Can we use custom base directory?
E.g. I tried this, but it didn't work:
1 2 3 4 | local fileName = "file.xml" local filePath = "/data/data/com.some.other.app/" local path = system.pathForFile(fileName, filePath) |
I was actually able to open file and write to it, all without errors. But no actual file was created in that directory.
EDIT: never mind, I check the path created by system.pathForFile and it was wrong: "/data/data/com.my.app/files/coronaResources/file.xml"
So I just called io.open(filePath + fileName, "w+"), but it gave an error. Probably, root permissions are needed.
Files are sandboxed and your are not allowed to access files like that. system.pathForFile is intended to be used with the Corona support file paths.
Please post your questions in the forums and not on the documentation pages. "All bug reports and support questions will be removed from this area."
Thanks.
Anyone know where this actually writes to on an android device? I've used astro to search for my files and can't find them.