B4J Code Snippet Redirect the output to a file

Name: RedirectOutput

Description: This sub will redirect the program output to a file.

B4X:
Sub RedirectOutput (Dir As String, FileName As String)
   #if RELEASE
   Dim out As OutputStream = File.OpenOutput(Dir, FileName, False) 'Set to True to append the logs
   Dim ps As JavaObject
   ps.InitializeNewInstance("java.io.PrintStream", Array(out, True, "utf8"))
   Dim jo As JavaObject
   jo.InitializeStatic("java.lang.System")
   jo.RunMethod("setOut", Array(ps))
   jo.RunMethod("setErr", Array(ps))
   #end if
End Sub

Usage:
B4X:
RedirectOutput(File.DirApp, "logs.txt")

If this app will be installed with an installer then you should not use File.DirApp as it will be read-only.
 

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi

How do i redirect to Standard output(Screen) again when i redirect it to file ?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Initialize ps with this code:
B4X:
Dim fd As JavaObject
fd.InitializeStatic("java.io.FileDescriptor")
Dim jout As JavaObject
jout.InitializeNewInstance("java.io.FileOutputStream", Array(fd.GetField("out")))
ps.InitializeNewInstance("java.io.PrintStream", Array(jout, True, "utf8"))
 

Mashiane

Expert
Licensed User
Longtime User
Name: RedirectOutput

Description: This sub will redirect the program output to a file.

B4X:
Sub RedirectOutput (Dir As String, FileName As String)
   #if RELEASE
   Dim out As OutputStream = File.OpenOutput(Dir, FileName, False) 'Set to True to append the logs
   Dim ps As JavaObject
   ps.InitializeNewInstance("java.io.PrintStream", Array(out, True, "utf8"))
   Dim jo As JavaObject
   jo.InitializeStatic("java.lang.System")
   jo.RunMethod("setOut", Array(ps))
   jo.RunMethod("setErr", Array(ps))
   #end if
End Sub

Usage:
B4X:
RedirectOutput(File.DirApp, "logs.txt")

If this app will be installed with an installer then you should not use File.DirApp as it will be read-only.
I'm getting... failed to initialize logger
 

tufanv

Expert
Licensed User
Longtime User
Name: RedirectOutput

Description: This sub will redirect the program output to a file.

B4X:
Sub RedirectOutput (Dir As String, FileName As String)
   #if RELEASE
   Dim out As OutputStream = File.OpenOutput(Dir, FileName, False) 'Set to True to append the logs
   Dim ps As JavaObject
   ps.InitializeNewInstance("java.io.PrintStream", Array(out, True, "utf8"))
   Dim jo As JavaObject
   jo.InitializeStatic("java.lang.System")
   jo.RunMethod("setOut", Array(ps))
   jo.RunMethod("setErr", Array(ps))
   #end if
End Sub

Usage:
B4X:
RedirectOutput(File.DirApp, "logs.txt")

If this app will be installed with an installer then you should not use File.DirApp as it will be read-only.
where should we direct it if it is installed via installer ?
 

Dadaista

Active Member
Licensed User
Longtime User
Initialize ps with this code:
B4X:
Dim fd As JavaObject
fd.InitializeStatic("java.io.FileDescriptor")
Dim jout As JavaObject
jout.InitializeNewInstance("java.io.FileOutputStream", Array(fd.GetField("out")))
ps.InitializeNewInstance("java.io.PrintStream", Array(jout, True, "utf8"))

Above is not working for me
 
Top