0

I am trying to read txt file using RandomAccessFile and FileChannel which contains large number of float / integer values, but at after the all conversations using ByteBuffer the values which I get as a result are not the same with the ones in txt file. Here is how I am doing it :

        RandomAccessFile mRandomFile = new RandomAccessFile(Environment.getExternalStorageDirectory() + "/Models/vertices.txt", "rw");
        FileChannel mInChannel = mRandomFile.getChannel();
        ByteBuffer mBuffer = ByteBuffer.allocate(181017 * 4);
        mBuffer.clear();

        mInChannel.read(mBuffer);

        mBuffer.rewind();
        FloatBuffer mFloatBUffer = mBuffer.asFloatBuffer();
        mFloatBUffer.get(VERTS);

        mInChannel.close();

        for (int i = 0; i < 20; i++) {
            Log.d("", "VALUE: " + VERTS[i]);
        }

The values in txt file are presented in this way (they are separated with a new line):

-36.122300
-6.356030
-46.876744
-36.122303
-7.448818
-46.876756
-36.122303
-7.448818
81.123221
-36.122300
-6.356030
81.123209
36.817676
-6.356030
-46.876779
36.817676
-7.448818
-46.876779
-36.122303
-7.448818

and the values which I am getting in for are:

VALUE: 1.0187002E-11
VALUE: 2.5930944E-9
VALUE: 6.404289E-10
VALUE: 2.5957827E-6
VALUE: 2.6255839E-6
VALUE: 8.339467E-33
VALUE: 4.1885793E-11
VALUE: 1.0740952E-5
VALUE: 1.0187002E-11
VALUE: 2.5930944E-9
VALUE: 6.513428E-10
VALUE: 1.0383363E-5
VALUE: 4.3914857E-5
VALUE: 8.339467E-33
VALUE: 4.1885793E-11
VALUE: 1.0801023E-5
VALUE: 1.0187002E-11
VALUE: 2.5930944E-9
VALUE: 6.513428E-10
VALUE: 1.0383363E-5

Any ideas, suggestions what I am missing here?

user207421
  • 305,947
  • 44
  • 307
  • 483
hardartcore
  • 16,886
  • 12
  • 75
  • 101
  • It is a text file, not a file full of floats in binary format. You're using the wrong tools for the job. – user207421 Sep 16 '14 at 10:20
  • Ok, so the question is, how I should create this binary file full with floats? I've seen some examples with RandomAccessFile using txt files, that's why I tried this option in this way. – hardartcore Sep 16 '14 at 10:21

2 Answers2

5

It is a text file, not a random access file. You should be reading with a BufferedReader. It´s got a readLine() that returns a String, and then you can just go with Double.valueOf(String).

There´s more code here How to use Buffered Reader in Java

Community
  • 1
  • 1
eduyayo
  • 2,020
  • 2
  • 15
  • 35
  • I've tried this and it works, but it's too slow for my needs. So i guess I should create another specific file (random-access) to be able to get the data as it is, right? – hardartcore Sep 16 '14 at 10:21
  • I don´t know what you want to achieve with that. If you just want float or double numbers in a file you should create that with a DataOutputStream http://docs.oracle.com/javase/7/docs/api/java/io/DataOutputStream.html and read it with a DataInputStream will be the hell out faster than parsing text files. If you want to draw those points on screen, maybe you should move to some out-of-the-box solution such as SVG for 2d gfx or OBJ or 3DS for 3d models. – eduyayo Sep 16 '14 at 10:25
  • I am using Vuforia for showing 3d models, that's why I need the points for drawing. BufferedReader and DataOutputStream are too slow for loading 12 models where everyone of them has 4 files with like 200 000 float / integer values. That's why I am using FileChannel, which opens and read 4 files for less than a second. – hardartcore Sep 16 '14 at 10:31
1

Android-Developer again, i see you try the binary-file-approach... to do this you must convert your data

Create a new Java-Project and use your original methods there to load the the values from a xml/text-file and convert it into floats... (yes, it will take some time then, but it provided valid data...) once you have the data inside your application store your floats into a file named floats.bin (using a FileWriter).

Go then and copy the file floats.bin into your Android Project and try to load it there with your code from above (looks good in my opinion)...

(referring to Android fastest way of reading big arrays of variables) after sleeping one night over your problem i think you can

  • either distract the user and load the data before you show your models...

  • or you can show the model while you are loading - and let the user see your progress and see the model growing with each second....

  • or you can split your data into seperate chunks from coarse to fine and load first the coarse data and show it - then you load into background the finer and finer data and add it piece by piece into your model...

Community
  • 1
  • 1
Martin Frank
  • 3,445
  • 1
  • 27
  • 47
  • The problem is that the models are not going to be represent to the user, only if he scans a unique image marker which will load the right model. So if user is in front of the marker and opens the application, he have to wait a lot of time to load the models all of them, because the scan - show model should be really fast process...not more than a second. – hardartcore Sep 16 '14 at 11:07
  • ok, then you really have to load the model before showing... i didn't want be impolite when making those suggestion, sorry... but maybe my hint with having these two projects helps you out a little bit... – Martin Frank Sep 16 '14 at 11:10
  • I will try and post an update with the result, thanks for the suggestions! – hardartcore Sep 16 '14 at 11:19
  • i would be really glad to see your progress! and i really wish you good luck and much much success!!! – Martin Frank Sep 16 '14 at 11:23
  • Just one question i'm having now... have you ever considered to store the whole 3d-model instead of all points of that model? – Martin Frank Sep 16 '14 at 13:13
  • Vuforia is reading the model with it's points, I am not doing like this, because I want it that way. And just for the record, it is working now, I am just a little busy to post the solution. Thanks for the help! – hardartcore Sep 16 '14 at 13:24
  • wow - i'm really glad that you found y solution, you where having trouble with this for several days!!! *cheers* – Martin Frank Sep 16 '14 at 13:26
  • thanks for remembering me and accepting! i hope we can collaborte further sooner or later!!! good luck and much much success for you and your app!!! – Martin Frank Sep 17 '14 at 08:44