B4J Question Random file read?

Troberg

Well-Known Member
Licensed User
Longtime User
I'm trying to read some data from a specific position in some files, but I can't get it to work. I have a string feeling that I'm trying to do this the wrong way altogether and am just complicating it needlessly.

This is my code:

B4X:
Private Sub GetFile() As String
    Dim bc As ByteConverter
    Dim numfiles As Int==(File.Size("y:\p\clips","clips.ix")-4)/4
    
    'Get a random number, based on the number of items in the file
    Dim slump As Int=Rnd(0,numfiles-1)
    
    'Read from index file. It's a simple list of indexes for the main list file, with each index pointing at the start of each record, except the last, which points at the end of the last record. We read the start and end index of the records indicated by slump.
    Dim instix As InputStream=File.OpenInput("y:\p\clips","clips.ix")
    Dim bytesix(8) As Byte
    Dim countix As Int=instix.ReadBytes(bytesix,slump*4,8) 'Fails here!!!
    instix.close
        
    Dim ix() As Int=bc.IntsFromBytes(bytesix)
    
    'Now, we should have the start and end index, use that to read the record.
    Dim instfil As InputStream=File.OpenInput("y:\p\clips","clips.lst")
    Dim bytesfil(ix(1)-ix(0)) As Byte
    Dim countfil As Int=instfil.ReadBytes(bytesfil,ix(0),ix(1)-ix(0))
    instfil.close
    
    Return bc.StringFromBytes(bytesfil,"UTF8")
End Sub

This works as long as I access the first record, but with any other record, it fails at the indicated line.

This is the error:

Error occurred on line: 158 (Main)
java.lang.reflect.InvocationTargetException
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.shell.Shell.runGoodChain(Shell.java:373)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:181)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
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:91)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA$2.run(BA.java:230)
at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(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$152(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IndexOutOfBoundsException
at java.io.BufferedInputStream.read(BufferedInputStream.java:338)
at anywheresoftware.b4a.objects.streams.File$InputStreamWrapper.ReadBytes(File.java:475)
at b4j.example.main._getfile(main.java:315)
at b4j.example.main._astream_newdata(main.java:198)
... 21 more
Error occurred on line: 158 (Main)
java.lang.NullPointerException
at anywheresoftware.b4a.shell.Shell.runGoodChain(Shell.java:373)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:181)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
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:91)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA$2.run(BA.java:230)
at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(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$152(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
java.lang.RuntimeException: java.io.EOFException
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:120)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA$2.run(BA.java:230)
at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(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$152(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:267)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:229)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
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:91)
... 9 more

This makes my brain hurt, any suggestions?

(Sorry for the messy code, I'm just trying to get this to work before I put it into the real program.)
 

thetahsk

Active Member
Licensed User
Longtime User
I'm trying to read some data from a specific position in some files, but I can't get it to work. I have a string feeling that



B4X:
Private Sub GetFile() As String
    Dim bc As ByteConverter
    Dim numfiles As Int==(File.Size("y:\p\clips","clips.ix")-4)/4

check your code, there is no == operator in b4j
 
Upvote 0

emexes

Expert
Licensed User
I've always used the jRandomAccessFile library:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    'MainForm.Show
 
    Dim raf As RandomAccessFile
    raf.Initialize("c:\stuff", "samplefile.txt", True)
 
    Dim Buffer(10) As Byte
    raf.ReadBytes(Buffer, 0, Buffer.Length, 5)    'read ten bytes from offset 5 (sixth byte of file)
 
    Log( BytesToString(Buffer, 0, Buffer.Length, "utf8") )
 
End Sub
 
Last edited:
Upvote 0

Troberg

Well-Known Member
Licensed User
Longtime User
check your code, there is no == operator in b4j

That's an editing mistake when I added the code here. At first, I had an =0'=(File.Size("y:\p\clips","clips.ix")-4)/4, and when I fixed that, I messed up. Problem still remains.

I've always used the jRandomAccessFile library:
(snip)

Thanks, I'll try that!

Edit: Yep, that worked! You are my hero!
 
Last edited:
Upvote 0

thetahsk

Active Member
Licensed User
Longtime User
You can also use the InputStreamToBytes method.

B4X:
Dim b() As Byte = Bit.InputStreamToBytes(File.OpenInput("C:\Anyware", "index.ix"))
Log(BytesToString(b, 10,15, "utf8")) 'Read 15 bytes starting from 10
 
Upvote 0
Top