The user creates a directory with the name, I need to make it so that if the directory in which he creates a new directory already contains such a name, the function returns true and vice versa if there is no such directory yet ...

I understand that you need to read all the directory names in the array and then compare with the name that the user wants to create, but how to read all the names in the array?

I started writing a function, but I can't figure out how to read the names.

 private boolean isContains(String str) { boolean result = false; String path = UtilClass.getAvatarPath(context).toString(); File file = new File(path); return result; } 

Tell me how to do this?

  • one
    There (File class) there is something like listFiles, will return a list of all files. And the list has a contains method - YuriySPb

2 answers 2

As a result, I created for myself such a method that checks whether the folder is contained at the specified address or not

 public static boolean isNameFree(Context context, String name) { final String path = Environment.getExternalStorageDirectory().getAbsoluteFile() + "/Android/data/com.example.android.camera2basic/files" + "/" + getEmail(context) + "/AvatarPackage/" + name; File file = new File(path); return !file.exists(); } 
  • 3
    I’ll bother you again. It seems to me that at the end you need to add! before file. And the rest, everything seems to be in order, since the full path cannot be matched. - iramm
  • @iramm If we assume that the method checks whether the name for the folder is free, judging by the name, then you are right. - pavlofff
  • @iramm Thank you) Added in reply) - Aleksey Timoshchenko

If I understand correctly,

 file.exists() 

You should be fine. If true , then there is such a directory, if false then no

  • Yes, that's right ... as I have complicated everything. Thank you - Aleksey Timoshchenko
  • @AlekseyTimoshchenko From the docks: "file.exists () ... That is, it is not a specific folder, but a file system. And besides, it is not checked whether the value that matches the name is a file or folder. That is, bugs are possible in the future. What do you think? - iramm
  • @iramm didn't quite understand why bugs are possible? In my app it works like this, there is a folder for the folders of pictures of a particular avatar. That is, if a new avatar is created, then for all its pictures a folder in the folder for pictures will be created and all these files will be sent there to save. The only thing I needed to check is when the user creates a new folder for avatar pictures, is there already a folder with that name or not ... What did you mean? - Aleksey Timoshchenko
  • @AlekseyTimoshchenko I find it difficult to drive into the specifics of your task. I meant that since it’s about the file system (true, what does underlying?), Then if somewhere else (not in your folder) there is already a folder with the name you are looking for, then the specified method will return true, although This directory has not yet been created in the folder you are interested in. One hope for the underlying file system. Perhaps this term refers to the current folder and its attachments. Then the danger is less or taking into account your specifics, maybe it is not at all.)) - iramm
  • @iramm posted his answer. In my case, I do not check the name on the entire file system, only in a specific directory - Aleksey Timoshchenko