B4J Question How to check if file could be created in selected directory ?

agb2008

Member
Licensed User
Longtime User
Very simple question - am I correct that there is no built in function that could check if file or directory could be created in selected directory ? Because command like File.MakeDir seems not returning any error value... Am I correct ?

And it seems that possible workaround is to try creating new dir (or file) and later check if file was created and if so one have to delete test directory or file...

If I am correct - could functionality of file/directory creation functions be extended - so that they at least return True if file/dir could be created and False if it could not be created ?
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
That is correct but i think it is not that hard to do either check:

first returns false because DirAssets is readOnly.
second returns true because DirApp is Writable.

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
  
  
    Log(checkFile(File.DirAssets))
    Log(checkFile(File.DirApp)) 
  
  
End Sub

public Sub checkFile(dir As String) As Boolean
    Return inline.RunMethod("checkFile",Array(dir))
End Sub

private Sub inline As JavaObject
Return Me
End Sub

#if java

import java.io.*;

static public Boolean checkFile(String filename) {
    File f = new File(filename);
    if(f.canWrite() == true) {
        return true;
    } else {
        return false;
    }
}  
#end if
 
Upvote 0
Top