1

I want to check if the asset in Assets directory of my Android App is a File or a Directory.

I have already tried every solution available anywhere on internet including StackOverflow.

Most of the solutions use

File myFile = new File(path);

then check for myFile.isDirectory(); or myFile.isFile();

But it works only if I have my files and directories anywhere else other than under the Assets directory.

Any suggestions will be highly appreciated.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
MindRoasterMir
  • 324
  • 1
  • 2
  • 18
  • 1
    so: what is it you are having trouble with? just saying new File("") won't test whether it's a directory or file, you'll need to do a bit more – Stultuske Mar 29 '18 at 09:32
  • Some solutions told that checking myFile.isDirectory() or myFile.isFile() would help but it didnot because it works only if it is a non Assests path. thanks – MindRoasterMir Mar 29 '18 at 09:40
  • normally, that should work just fine. – Stultuske Mar 29 '18 at 09:43
  • @stultuske but they did not work I spent more than 6 hours trying them. isDirectory(); and isFile(); always returned false. They accepted no formation of --- path ---. As I told they work only with non Assests paths. reason is Assests is not part of File System as some solutions suggested. Thanks – MindRoasterMir Mar 29 '18 at 09:48
  • problem is, your 100% working solution has a few major flaws in it. it may work for now, but who knows what files/directories will be added later that 'll give a false positiive? – Stultuske Mar 29 '18 at 09:49
  • here is how someone else overcame the problem: https://stackoverflow.com/questions/4447477/how-to-copy-files-from-assets-folder-to-sdcard – Stultuske Mar 29 '18 at 09:51
  • there are two problems in a solution that you are suggesting as a possible answer. 1. Its title does not coordinate with my problem and solution so it will not help anyone with same problem as I was facing 2. It did not list files and checked if they were files or directories in Assets folder. Thanks – MindRoasterMir Mar 29 '18 at 10:07

2 Answers2

1

Here is a solution to my problem that I found out working 100% listing all directories and files even sub-directories and files in subdirectories. It also works for multiple levels.

Note: In my case

  1. Filenames had a . in them. i.e. .htm .txt etc
  2. Directorynames did not have any . in them.

Note: about -- path --

  1. to get all files and directories in Assets root folder

    String path = ""; // assets

  2. or to get files and directories in a subfolder use its name as path

    String path = "html"; // <<-- assets/html

    listAssetFiles2(path); // <<-- Call function where required
    //function to list files and directories
    public void listAssetFiles2 (String path){
    String [] list;
    
    try {
        list = getAssets().list(path);
        if(list.length > 0){
            for(String file : list){
                System.out.println("File path = "+file);
    
                if(file.indexOf(".") < 0) { // <<-- check if filename has a . then it is a file - hopefully directory names dont have . 
                    System.out.println("This is a folder = "+path+"/"+file);
                    if(path.equals("")) {
                        listAssetFiles2(file); // <<-- To get subdirectory files and directories list and check 
    
                    }else{
                        listAssetFiles2(path+"/"+file); // <<-- For Multiple level subdirectories
                    }
                }else{
                    System.out.println("This is a file = "+path+"/"+file);
                }
            }
    
        }else{
            System.out.println("Failed Path = "+path);
            System.out.println("Check path again.");
        }
    }catch(IOException e){
        e.printStackTrace();
    }
    

    }

Thanks

MindRoasterMir
  • 324
  • 1
  • 2
  • 18
  • this doesn't work 100%. it is possible to create a file without extension. Also: filenames can have a '.' in them. – Stultuske Mar 29 '18 at 09:41
  • @stultuske it worked for me 100%. I have a folder where I have .htm extension files and directories wihtout and . (dot) in them. Also please read your comment again. You maybe wanted to say "Directorynames can have a '.' in them" . Please recheck your comment thanks – MindRoasterMir Mar 29 '18 at 09:51
  • Yes you are right but it provides the basic Idea and can be ammended as per the requirement of each programmer. This is what worked for me in my given problem. Thanks. – MindRoasterMir Mar 29 '18 at 10:05
  • This is a bad solution as it assumes a dot or not. And also there can be files names that have no dot dot. – greenapps Mar 29 '18 at 14:19
  • Instead of letting a dot in a name determine what should happen you should check if it is a file or a directory. Make your own `isDirectory(name)` function by trying to open an InputStream for the file/directory. If you succeed it is a file. If you get an exception its a directory. Dont forget to close the stream when done. – greenapps Mar 29 '18 at 14:22
  • @greenapps everything is mentioned in the description. For my asked question I found no working answer anywhere under given circustances. This WORKS! – MindRoasterMir Mar 29 '18 at 14:22
  • Yes for you. But it is a bad solution. Just make that little function! Ten lines or less will do. – greenapps Mar 29 '18 at 14:24
  • @greenapps Thanks for your effort. I will try it. If you could provide a working example, it will be appreciated and helpfull for the other users. Thanks – MindRoasterMir Mar 29 '18 at 14:24
  • `and check if an asset is a file or a folder` Well you have that in the subject of your post. And you pretent that your answer covers it. Well no. You do not check for folder or file. You only check for a dot in a name. Thats no answer. So try to do better by implementing isDirectory(). – greenapps Mar 29 '18 at 14:29
0

Instead of letting a dot in a name determine what should happen you should check if it is a file or a directory. Make your own isDirectory(name) function by trying to open an InputStream for the file/directory. If you succeed it is a file. If you get an exception its a directory. Dont forget to close the stream when done.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • Please provide some coffee if you have some spare time. Thanks – MindRoasterMir Mar 29 '18 at 14:55
  • If you put files in assets then sooner or later you will use them in your app. The only way to use/read them is opening an InputStream for it. You will have that code already. Use it! Only a few lines will do to implement `isDirectory(name)`. – greenapps Mar 29 '18 at 14:58
  • I already tried lots of variations nothing worked for me . Please provide code example whenever you can. Thanks. I will try again anyways. – MindRoasterMir Mar 29 '18 at 17:45