B4J Question Can not open file from drive C.

red30

Well-Known Member
Licensed User
Longtime User
When I open a file from File.DirApp. Everything works well.
B4X:
        fileChooser.Initialize
        fileChooser.InitialDirectory=File.DirApp
        fileChooser.SetExtensionFilter("txt", Array("*.txt"))
        NameForSave=File.GetName(fileChooser.ShowOpen(MainForm))
        lblStat.Text=(NameForSave)
        Log("Выбран файл: "&NameForSave)
        If NameForSave.Length>0 Then
            raf.Initialize(File.DirApp, NameForSave, False)
            TimerOTP.Initialize("TimerOTP",2)
            TimerOTP.Enabled=True
        Else
If I try to open a file for example from the drive "C: \"
B4X:
        fileChooser.Initialize
        fileChooser.InitialDirectory="C:\"
        fileChooser.SetExtensionFilter("txt", Array("*.txt"))
        NameForSave=File.GetName(fileChooser.ShowOpen(MainForm))
        lblStat.Text=(NameForSave)
        Log("Выбран файл: "&NameForSave)
        If NameForSave.Length>0 Then
            raf.Initialize("C:\", NameForSave, False)
            TimerOTP.Initialize("TimerOTP",2)
            TimerOTP.Enabled=True
        Else
B4X:
Выбран файл: ttt.txt
main._btnos_action (java line: 588)
java.io.FileNotFoundException: C:\ttt.txt (Отказано в доступе)
    at java.io.RandomAccessFile.open0(Native Method)
    at java.io.RandomAccessFile.open(RandomAccessFile.java:316)
    at java.io.RandomAccessFile.<init>(RandomAccessFile.java:243)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.Initialize2(RandomAccessFile.java:72)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.Initialize(RandomAccessFile.java:65)
    at b4j.liana.write.novinka.wr.main._btnos_action(main.java:588)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
    at anywheresoftware.b4a.BA$1.run(BA.java:215)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
How do I open a file that is on the drive "C: \" for example?
 

red30

Well-Known Member
Licensed User
Longtime User
TimerOTP performs another function. I do not need it to delay.
Log(NameForSave) prints the name of the file. (It was necessary for debugging)
B4X:
Sub TimerOTP_Tick
    ch1=raf.ReadUnsignedByte(chet)
    ch2=raf.ReadUnsignedByte(chet+1)
    ch3=raf.ReadUnsignedByte(chet+2)
    ch4=raf.ReadUnsignedByte(chet+3)
    ch5=raf.ReadUnsignedByte(chet+4)
          
    ch1=MyASC(ch1)
    ch2=MyASC(ch2)
    ch3=MyASC(ch3)
    ch4=MyASC(ch4)
    ch5=MyASC(ch5)
          
    lblStat.Text=ch1*10000+ch2*1000+ch3*100+ch4*10+ch5
  
    MB=ch1*10000+ch2*1000+ch3*100+ch4*10+ch5
    SB=Bit.ShiftRight((ch1*10000+ch2*1000+ch3*100+ch4*10+ch5),8)
  
    chet=chet+6
    KS=KS-1
    If KS=0 Then
        TimerOTP.Enabled=False
        lblStat.Text="Отправленно"
        chet=0
    End If
    astream.Write(Array As Byte(SB,MB))
End Sub
The program must open the file I selected to transfer data from it.
When it was so everything worked, but only with File.DirApp.
B4X:
        fileChooser.Initialize
        fileChooser.InitialDirectory=File.DirApp
        fileChooser.SetExtensionFilter("txt", Array("*.txt"))
        NameForSave=File.GetName(fileChooser.ShowOpen(MainForm))
        lblStat.Text=(NameForSave)
        Log("Выбран файл: "&NameForSave)
        If NameForSave.Length>0 Then
            raf.Initialize(File.DirApp, NameForSave, False)
            TimerOTP.Initialize("TimerOTP",2)
            TimerOTP.Enabled=True
        Else
          
        End If
I want to choose a file from anywhere ...
In the picture, an example of such a file
 

Attachments

  • Безымянный.png
    Безымянный.png
    70.4 KB · Views: 213
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
B4X:
        fileChooser.Initialize
        fileChooser.InitialDirectory=File.DirApp
        fileChooser.SetExtensionFilter("txt", Array("*.txt"))
        NameForSave = fileChooser.ShowOpen(MainForm)
        'NameForSave=File.GetName(fileChooser.ShowOpen(MainForm))
        lblStat.Text=(NameForSave)
        Log("Выбран файл: "&NameForSave)
        If NameForSave.Length>0 Then
            raf.Initialize(NameForSave, "", False)
            TimerOTP.Initialize("TimerOTP",2)
            TimerOTP.Enabled=True
        Else
          
        End If
B4X:
Выбран файл: C:\ttt.txt
main._btnos_action (java line: 588)
java.io.FileNotFoundException: C:\ttt.txt (Отказано в доступе)
    at java.io.RandomAccessFile.open0(Native Method)
    at java.io.RandomAccessFile.open(RandomAccessFile.java:316)
    at java.io.RandomAccessFile.<init>(RandomAccessFile.java:243)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.Initialize2(RandomAccessFile.java:72)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.Initialize(RandomAccessFile.java:65)
    at b4j.liana.write.novinka.wr.main._btnos_action(main.java:588)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
    at anywheresoftware.b4a.BA$1.run(BA.java:215)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
No, I do not know where this text comes from ...
If the file is in File.DirApp, then everything works fine. But if in another folder (for example C:\), then such an error ...
 
Upvote 0
Top