B4J Tutorial Working with files

Status
Not open for further replies.
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:
B4X:
C:\Users\[user name]\AppData\Roaming\[AppName]
 
Last edited:

Theera

Well-Known Member
Licensed User
Longtime 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:

rapblack

Member
Licensed User
Longtime 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
 

ericvanderhoeven

Member
Licensed User
Longtime User
The File methods in B4J are identical to Basic4android 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.

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
 

ericvanderhoeven

Member
Licensed User
Longtime User
You can create a recursive function that searches in all folders. However it can be quite slow to search all the disk.

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
 

howard bassen

Member
Licensed User
Longtime 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?
 
Status
Not open for further replies.
Top