B4J Question List of all the files in the File.Assets directory

wdegler

Active Member
Licensed User
Longtime User
How can a create a list of all the files in the File.Assets directory?
"File.ListFiles" will not work for this directory.
I need this for my present app development.
Help.
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

one way is by using Inline Java BUT NOTE: See reply #2 = It is not possible to list the asset files.
The solution below is the same as using File.ListFiles with path given.

B4X:
#If JAVA
import java.io.File;

//List the files from a folder and return as stringarray
/*
    Example:
    Private joFiles As JavaObject = Me
    'Get all files from the dirassets folder
   Dim f() As String = joFiles.RunMethod("ListFiles", Array("<PATHtoFILESfolder>"))
    If f = Null Then Return
    For i = 0 To f.Length - 1
        Log(f(i))
    Next
*/
public static String[] ListFiles(String searchpath) {
    File f = null;
    String[] paths;
    try {
        f = new File(searchpath);
        paths = f.list();
        return paths;
     } catch(Exception e){
        e.printStackTrace();
        return null;
    }
}

#End If
 
Last edited:
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi Erel,

Thanks for the hint = Have tested the code in release mode, also run the app from the command window. No issues encountered. Files are listed as expected.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Understand now = Thanks
To use the path has to be set to the files folder by
not using
B4X:
Dim f() As String = joFiles.RunMethod("ListFiles", Array(File.DirApp.Replace("Objects", "Files")))

but use
like
B4X:
   Dim f() As String = joFiles.RunMethod("ListFiles", Array("PATHtoASSETSfolder"))

BUT then File.ListFiles can also be used with out inline Java. Have made a note in post #1.
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
If your .jar file is called MyJar.jar, do as follows:
B4X:
Dim arch as Archiver   'Search the forum for the Archiver library and download it
File.Copy(File.DirApp, "MyJar.jar", File.DirApp, "MyJar.zip")
arch.UnZip(File.DirApp, "MyJar.zip", File.DirApp & "\MyJarFile", "arch")
Dim fl As List = File.ListFiles(File.DirApp & "\MyJarFile\Files")
For j = 0 To fl.Size-1
    Log(fl.Get(j))
Next
Make sure to delete MyJar.zip and the entire MyJarFile directory when you're done.
 
Upvote 0

wdegler

Active Member
Licensed User
Longtime User
I thank all of you for your responses. I am still working on this matter but your replies are helpful and will save me time.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Explored and tested a further solution by reading the content of the Jar file - again using Inline Java.

B4X:
#If JAVA
import java.io.*;
import java.util.*;
import java.util.zip.*;

//List the content of a jar archive
//Example of getting the File Assets from a Jar - which are stored in the Files folder:
//    Private joFiles As JavaObject = Me
//    Dim f As List = joFiles.RunMethod("JarContent", Array("<Here FullPathToTheJarFile>"))
//    If f = Null Then Return
//    Log("Total number of files found in the Jar: " & f.size)
//    Log("Asset Files:")
//    For i = 0 To f.size - 1
//        Dim s As String = f.Get(i)
//        If s.StartsWith("Files") Then Log(f.Get(i))
//    Next
//
public static List<String> JarContent(String searchpath) {
    List<String> files = new ArrayList<String>();
    try {
        ZipFile zipFile = new ZipFile(searchpath);
        Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
        while (zipEntries.hasMoreElements()) {
            files.add(zipEntries.nextElement().getName());
        }
        return files;
    } catch (IOException ioe) {
        ioe.printStackTrace();
        return null;
    }
}
 
Last edited:
Upvote 0
Top