B4J Question Create text file.

ciprian

Active Member
Licensed User
Longtime User
Hi EREL. I want to create a texfile, and write into it some text.
I have a variable "i" and i have to write some lines for 0 to 7 per example.
This is how i have done as far.
B4X:
#Region  Project Attributes
    #MainFormWidth: 500
    #MainFormHeight: 500
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim Writer As TextWriter
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    Act
End Sub

Sub Act
    Writer.Initialize(File.OpenOutput(File.DirDefaultExternal, "1.txt", False))
    For i = 0 To 7
        Writer.WriteLine("/*  */")
        Writer.WriteLine("ALM_NUM: " & i)
        Writer.WriteLine("ALM_OUT: 0xF")
        Writer.WriteLine("ALM_MASK: 0")
        Writer.WriteLine("ALM_CHG: 0")
        Writer.WriteLine("ALM_STATUS: 0x11")
        Writer.WriteLine("ALM_SCNEW:      begin")
        Writer.WriteLine("                        info")
        Writer.WriteLine("                end")
        Writer.WriteLine("ALM_SCOLD:      begin")
        Writer.WriteLine("                        info")
        Writer.WriteLine("                end")
    Next
    Writer.Close
End Sub

How do i create this "1.txt" file, where do i find it, and how to make it work.
Thank you.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
See this tutorial: Working with files

B4X:
Dim lines As List
lines.Initialize
lines.Add("/* */")
lines.Add("ALM..."
...
File.WriteList(File.DirApp, "1.txt", lines)
The file will be created in jar folder (Objects folder if you run from the IDE).

Another option:
B4X:
File.WriteList("", "C:\Some folder\1.txt", lines)

You can use FileChooser to let the user choose the path. It is used in the smiley example.
 
Upvote 0
Top