Passing a Large CSV or Excel file containing float values to a Java MappedByteBuffer without having to separate values one by one or line by line

133 views Asked by At

I am trying to pass a Large CSV with float values to a MappedByteBuffer. I want it to be read as float values instead of text and dont want to have to parse it character by character and remove commas (delimiters). I want to process it as a 2D array (managed by index) without using any memory i.e. not putting it into any variable for traversing etc. The code I am working with is:-

private static String bigExcelFile = "testCSV.csv";
RandomAccessFile file = new RandomAccessFile(new File(bigExcelFile), "r")
FileChannel fileChannel = file.getChannel();
MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
for (int i = 0; i < buffer.limit(); i++)
{
   System.out.println((char) buffer.get());
}  

I have tried using a floatBuffer too but it doesnt work either. I do understand that its a text file with values separated by comma, and I am willing to use an excel file instead. But the questions is how can I use the float data without having to put everything in the memory before or after putting it in the mappedbytebuffer, something we are actually trying to avoid by using memory mapped data structure).

Edited: What I did was parsed the CSV and wrote it onto a binary (.bin) file of floats and used that later to directly read data from that (.bin) file via MappedByteBuffer (asFloatBuffer) in the actual program. This eliminated string parsing and float casting during execution.

0

There are 0 answers