1. Black Friday Sale - 50% off

    B4A Standard for $29 instead of $59!
    Buy Now
    Dismiss Notice

Android Tutorial Text files

Discussion in 'Tutorials & Examples' started by Erel, Nov 28, 2010.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this question.
     
  2. rworner

    rworner Member Licensed User

    I am developing for a rooted MK802 mini PC. I have created a local.prop file that I can manually instal via a file manager, but want to automate the process using the code below.

    WhenI run this I get the following error:
    FileNotFoundException: /data/local.prop open failed EACCES (Permission Denied)
    -----------------------------

    Sub Globals
    Dim tw As TextWriter
    End Sub
    Sub Activity_Create(FirstTime As Boolean)

    Dim s1, sr1, sr2, sr3 As String
    s1="local.prop"
    sr1="service.adb.tcp.port=5555"
    sr2="ro.kernel.android.bootanim=0"
    sr3="debug.sf.nobootanimation=1"
    If File.Exists("/data/",s1)= False Then
    tw.Initialize(File.OpenOutput("/data/",s1,False))
    tw.WriteLine(sr1)
    tw.WriteLine(sr2)
    tw.WriteLine(sr3)
    tw.Close
    End If
    End Sub
     
  3. eps

    eps Well-Known Member Licensed User

    Ideally you should start a new thread for a Q like this, but to try and help the problem is the location of the file, you need
    to tell it where it is, it should be located under the directories that form part of your App, such as :

    Code:
    TextWriteLine.Initialize(File.OpenOutput(File.DirInternal, "yourfile.txt"False))
    I think it would be best to use the DirInternal, then prepend the /data/ part to your file name..

    HTH
     
  4. rworner

    rworner Member Licensed User

    Tr
    I tried the suggestion, no luck.
    I also tried a SU account --but instead of creating /data/local.prop it created /<packagename>/local.prop

    Sub Activity_Create(FirstTime As Boolean)

    Dim Command, Runner As String
    Dim StdOut, StdErr As StringBuilder
    Dim Result As Int
    Dim Ph As Phone
    StdOut.Initialize
    StdErr.Initialize
    Runner = File.Combine(File.DirInternal, "runner")
    Command = File.Combine(File.DirInternal, "command")
    File.WriteString(File.DirInternalCache, "runner", "su < " & Command)
    File.WriteString(File.DirInternalCache, "command", "echo service.adb.tcp.port=5555 >> /data/local.prop" & CRLF & "exit") 'Any commands via crlf, and exit at end
    Result = Ph.Shell("sh", Array As String(Runner), StdOut, StdErr)
    Log("Result: " & Result)
    Runner = File.Combine(File.DirInternal, "runner")
    Command = File.Combine(File.DirInternal, "command")
    File.WriteString(File.DirInternalCache, "runner", "su < " & Command)
    File.WriteString(File.DirInternalCache, "command", "echo ro.kernel.android.bootanim=0 >> /data/local.prop" & CRLF & "exit") 'Any commands via crlf, and exit at end
    Result = Ph.Shell("sh", Array As String(Runner), StdOut, StdErr)
    Log("Result: " & Result)
    Runner = File.Combine(File.DirInternal, "runner")
    Command = File.Combine(File.DirInternal, "command")
    File.WriteString(File.DirInternalCache, "runner", "su < " & Command)
    File.WriteString(File.DirInternalCache, "command", "echo debug.sf.nobootanimation=1 >> /data/local.prop" & CRLF & "exit") 'Any commands via crlf, and exit at end
    Result = Ph.Shell("sh", Array As String(Runner), StdOut, StdErr)
    Log("Result: " & Result)
    End Sub
     
  5. eps

    eps Well-Known Member Licensed User

    I can't see in your code where it carries out the FileOpen part...??

    Can you enclose the above in
    "[ code ]"

    "[ /code ]"

    tags please?
     
  6. djveleno

    djveleno Active Member Licensed User

    Hi Klaus,
    I created an app that has many pictures in different folders, how can I load images from these folders?
    I used this code, but it does not work:
    Code:
    ImageView1.Bitmap = LoadBitmap(File.DirAssets &/"automibili", ImageFiles(0))
    Thanks for all.
     
  7. klaus

    klaus Expert Licensed User

    Unfortunately you don't give enough information to help you !
    What have you done, don't you have a project showing the problem.
    What doesn't work ?
    Do you get any error messages ?
    Does the folder File.DirAssets &/"automibili" exist ?
    What is ImageFiles(0) and how is it defined ?
    Well, it's almost impossible to help whithout knowing the answeres to these questions !
    The best way : post your project as a zip file (IDE menu File / Export As Zip) so we could see what you have done and how and test your project in the same conditions as you do.

    Best regards.
     
  8. dieterp

    dieterp Active Member Licensed User

    I use a List to store a recordset using the DBUtils.ExecuteMemoryTable function. When I use File.WriteList to write that List to a text file, it writes the actual arraytype ([Ljava.lang.String;@42397b68) and not the entries I retrieve from the database. Is it possible to write a recordset that would have several rows containing Name, Surname, Age etc. values to a text file?
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    Not automatically. You can however write it with RandomAccessFile.WriteObject / ReadObject or KeyValueStore.
     
  10. IslamQabel

    IslamQabel Active Member Licensed User

    Thanks Erel, very helpful......So i made a code that i can read the help from a files stored in "Files" folder instead typing a very long help in Msgbox..
    Code:
    sub Activity_Create

    Activity.AddMenuItem("HELP","MenuHelp")
    End Sub


    Sub MenuHelp_click
        
    Msgbox(File.ReadString(File.DirAssets, "help.txt"), "Help")
    End Sub
    Can i display the contents of a txt file in Msgbox with different color , different font and change the color of background ?
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this question.
     
  12. Kwame Twum

    Kwame Twum Active Member Licensed User

    Please guys, how do you read a text file from a remote computer/server?
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    You should start a new thread for this question. There are several ways (SMB, Http, FTP, ...).
     
    Kwame Twum likes this.
  14. agus mulyana

    agus mulyana Member Licensed User

    Dear all

    Execuse me, I want to open an EXCEL file (ex: boox1 from SD Card) , anyone can help me ? or give me a free sample/code for this ? thank you
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    Please don't make duplicate posts.
     
  16. schemer

    schemer Active Member Licensed User

    I have found this page and am trying to test it for saving a text file. The demo from page one seems to work until it gets to the last two subroutines and the screen goes blank. What is supposed to happen there and how do I access the file on my device? Here are the last two subs:

    Code:
    Sub WriteTextWriter
        
    Dim TextWriter1 As TextWriter
        TextWriter1.Initialize(
    File.OpenOutput(File.DirRootExternal, "Text.txt"False))
        
    For i = 1 To 10
            TextWriter1.WriteLine(
    "Line" & i)
        
    Next
        TextWriter1.Close
    End Sub

    Sub ReadTextReader
        
    Dim TextReader1 As TextReader
        TextReader1.Initialize(
    File.OpenInput(File.DirRootExternal, "Text.txt"))
        
    Dim line As String
        line = TextReader1.ReadLine  
        
    Do While line <> Null
            
    Log(line) 'write the line to LogCat
            line = TextReader1.ReadLine
        
    Loop
        TextReader1.Close
    End Sub
    Thanks,
    schemer

    edit: I think I see the answer: 'write the line to LogCat :)
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    Note that in most cases there is no reason to use TextReader and TextWriter. You should instead use File.WriteString or WriteList (and ReadString, ReadList).
     
    schemer likes this.
  18. schemer

    schemer Active Member Licensed User

    Thank you Erel for the added info. I will do some more studying. My Nexus 7 arrives today so I will have a dedicated test device. WooHoo! :p
    schemer
     
  19. haddad

    haddad Member Licensed User

    hi all
    i have a problemme i can write in a file txt but can't read it , may i have a permission for reading or what ?

    cordialement
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    Which error do you get when trying to read the file? Can you post the code?
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice