Hello! How to use data from File in Kotlin and del...
# getting-started
o
Hello! How to use data from File in Kotlin and delete the information after? I get OutOfMemoryExcetion. I send data every 5 seconds from the File to the server. The data all the time is updated. Then need to remove the data. Now I have this :
Copy code
val bytes = fileOut.readBytes().sliceArray(range)
And then I send the bytes. I do nothing with this after. As I understand JVM garbage collector should clean this data myself after a while but does it not always. I google this, I think I can use something like try-with-resources in Java -
use
. But this extension is unable for the Class. Do you have some advice for me? Thanks.
k
How large is the file? Right now you're reading the entirety of it into memory and then you slice a part of it, maybe look into a way to only read a part from the start?
o
It can be 2 gb or more file. I have just found info :
Copy code
This method is not recommended on huge files. It has an internal limitation of 2 GB byte array size.
k
Yeah it's not actually running out of memory, it's just that arrays in Java have maximum size.
o
I have a file and I need to get some part of the file and send to server. The part of the file all time is different. It can be on the beginning of the file and it can be the end of the file.
"maybe look into a way to only read a part from the start?" I don't know any function for that stuff...
I have a range and I need take data from the file by the range.
k
You can use some combination of InputStream.skip/read, or use
DataInputStream
which is a bit more ergonomic:
Copy code
val file = FileInputStream("test.txt")
val data = DataInputStream(file)

val result = ByteArray(100)
data.readFully(result, 1000, result.size)
This reads bytes
1000 until 1100
. Be careful about character encodings or unexpected eofs of course.
o
is it 'easy' for JVM ?
k
Well this should directly use the low level file apis, so there's no garbage generated for the JVM to clean up if that's what you mean.
For example the 1000 offset just calls straight to the OS and asks it to skip 1000 bytes.
o
ok, thank you, I will try this
h
For example, if you’re parsing binary data and you don’t want to read the entire file at once, you must create an input stream (for binary data) or an input stream reader (for strings) - the example below will read 16 bytes:
Might be an option
k
That documentation is wrong,
file.read(bytes)
does not read 16 bytes, it reads up to 16 bytes.
If you want to use streams manually you'll have to use
skip
to get where you want to be, and then repeatedly call
read
in a loop until you've read enough bytes. That's exactly what
DataInputStream.readFully
does.