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

Discussion in 'B4J Questions' started by wdegler, Jul 14, 2015.

  1. wdegler

    wdegler Active Member Licensed 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.
     
  2. rwblinn

    rwblinn Well-Known Member Licensed 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.

    Code:
    #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: Jul 15, 2015
  3. Erel

    Erel Administrator Staff Member Licensed User

    This code will not work when you compile the app in release mode (it is the same code as File.ListFiles).

    It is not possible to list the asset files. You can add a text file with these files and then read it at runtime.
     
  4. rwblinn

    rwblinn Well-Known Member Licensed 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.
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Take the compiled jar file to a different folder (the desktop for example) and run it. It will not work as the asset files are inside the jar file.
     
  6. rwblinn

    rwblinn Well-Known Member Licensed User

    Understand now = Thanks
    To use the path has to be set to the files folder by
    not using
    Code:
    Dim f() As String = joFiles.RunMethod("ListFiles"Array(File.DirApp.Replace("Objects""Files")))
    but use
    like
    Code:
    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.
     
  7. Roycefer

    Roycefer Well-Known Member Licensed User

    If your .jar file is called MyJar.jar, do as follows:
    Code:
    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.
     
  8. wdegler

    wdegler Active Member Licensed 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.
     
  9. rwblinn

    rwblinn Well-Known Member Licensed User

    Explored and tested a further solution by reading the content of the Jar file - again using Inline Java.

    Code:
    #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: Jul 15, 2015
    stevel05 likes this.
Loading...