B4J Tutorial Working with files

Discussion in 'B4J Tutorials' started by Erel, Nov 19, 2013.

  1. Erel

    Erel Administrator Staff Member Licensed User

    The File methods in B4J are identical to B4A File methods.

    Text file methods

    File.WriteString - Writes the given text to a new file.
    File.ReadString - Reads a file and returns it content as a string.
    File.WriteList - Writes all values stored in a list to a file. All values are converted to string type if required. Each value will be stored in its own line.
    Note that if a value contains the new line character it will saved over more than one line and when you read it, it will be read as multiple items.
    File.ReadList - Reads a file and stores each line as an item in a list.
    File.WriteMap - Takes a map object which holds pairs of key and value elements and stores it in a text file. The file format is known as Java Properties file: .properties - Wikipedia, the free encyclopedia
    The file format is not too important unless the file is supposed to be edited manually. This format makes it easy to edit it manually.
    One common usage of File.WriteMap is to save a map of "settings" to a file.
    File.ReadMap - Reads a properties file and returns its key/value pairs as a Map object. Note that the order of entries returned might be different than the original order.

    If you want to write objects or collections other than simple lists or maps then you can use RandomAccessFile.WriteObject / ReadObject or use KeyValueStore class.

    Folders

    File.DirAssets
    - The assets folder holds the files that were added to the Files tab. This is a virtual read-only folder. When you compile your program in Release mode the files are packaged inside the Jar files.

    File.DirTemp - A temporary folder.

    File.DirApp - The program folder. This is the path to the jar file. Note that on Windows it might be read-only (if the program is installed in Program Files).

    File.DirData - A folder suitable for writing files. On non-Windows it is the same as File.DirApp. On Windows it points to the user data folder. For example:
    Code:
    C:\Users\[user name]\AppData\Roaming\[AppName]
     
    Last edited: Dec 10, 2015
  2. Theera

    Theera Well-Known Member Licensed User

    How to reference to File.DirApp? (In windows refer to c\:, In Linux and Mac how to refer to ?) Please give me some simples.
     
    Last edited: Nov 30, 2013
  3. Erel

    Erel Administrator Staff Member Licensed User

    Not sure that I understand. File.DirApp is the way to reference the app folder. You can see the actual path with: Log(File.DirApp).
     
  4. rapblack

    rapblack Member Licensed User

    Hi Erel,
    For this topic. Are you have an Example?
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Several of the examples are using files. Do you have any specific question or example you will like to see?
     
  6. rapblack

    rapblack Member Licensed User

    I mean to code example for create new file, read to normal text file. If I have example code. I can apply these example code for my app.

    Thank you for your replied
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    'save some text to a file
    File.WriteString(File.DirApp, "1.txt""some text")
    'read the text
    Dim s As String = File.ReadString(File.DirApp, "1.txt")
     
    fabiojava likes this.
  8. rapblack

    rapblack Member Licensed User

  9. ericvanderhoeven

    ericvanderhoeven Member Licensed User

    Hi Erel,

    if possible, I'd like some tips on creating a BROWSE option for locating a JPG in any folder on a windows PC. I like to create a B4J jar that allows for browsing the JPG (that can be anywhere on the windows computer), edit it and then save-as (with a similar browse function and therefore potentially in another location). Comparable to your typical load and save-as function found in many windows programs. I've already made my first jar to load a ''static'' JPG (via File.Dirapp) so I'm getting exited here :)

    !תוֹדָה

    Eric
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    You can create a recursive function that searches in all folders. However it can be quite slow to search all the disk.
     
  11. ericvanderhoeven

    ericvanderhoeven Member Licensed User

    Eril, thank you for your answer.

    I'm not looking for something that exotic like a recursive scan or something. No, just a simple (manual) browse through folders/disks like the attached picture from e.g. Launch4J (the window that pops up when you want to load a CFG file). You select the drive/folder manually etc..

    Eric

    file_open.png
     
  12. Erel

    Erel Administrator Staff Member Licensed User

  13. howard bassen

    howard bassen Member Licensed User

    Hello,

    For FileChooser there is no simple explanation of how to use the function in B4A. if possible, could you point to an example or template in B4A?
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    You cannot use FileChooser in B4A. Only in B4J.
     
Loading...