Avatar of Member_2_2394978
Member_2_2394978Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

Access array from RandomAccessFile

Hi,

I have a problem with my RandomAccessFile code!

I save an array by writing each float at a time to a file, attached code.  I then read a section of the array with the attached method.  I am getting floats back with the method, although not the correct ones!  

Basically, I write all the floats to the file, with all the three dimensions as [1000][1000][100].  And then when I read a float, I want to return the window, which is the 100 elements at [x][y].

Any suggestions?
Thanks
James
// WRITING THE FLOATS
            for (int x = 0; x < header.iNeuron; x++) {
                for (int y = 0; y < header.iNeuron; y++) {
                    for (int z = 0; z < header.iWin; z++) {
                        try {
                            raf1.writeFloat(afNorm[x][y][z]);
                        } catch (IOException ex) {
                            Logger.getLogger(DataStore.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }
 
// ACCESS THE FLOATS    
    public float[] getWindow(int xIn, int yIn) {
        float result[] = new float[header.iWin];
        
        // get the norms from the file, populate the array, and return the window
        // seek first
        long pos = xIn * yIn * 4;
        try {
            rafNorms.seek(pos);
        } catch (IOException ex) {
            Logger.getLogger(DataStore.class.getName()).log(Level.SEVERE, null, ex);
        }
        // read the window
        for (int i = 0; i < header.iWin; i++) {
            try {
                result[i] = rafNorms.readFloat();
            } catch (IOException ex) {
                Logger.getLogger(DataStore.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return result;        
    }

Open in new window

Java

Avatar of undefined
Last Comment
CEHJ
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

At a quick glance, try not

long pos = xIn * yIn * 4;

but

long pos = xIn * yIn;
Avatar of Member_2_2394978

ASKER

Hmm, thats giving me very starnge floats, still not right ones though.
I had, this because I thought the seek worked in bytes, and there were four bytes for each float!
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Well that should get you to the start of the innermost float[100] if that's what you want
Avatar of Member_2_2394978

ASKER

Yeh.  I want to get to [x][y][0] - and then i read 100 floats!
Avatar of Member_2_2394978

ASKER

Although it is not giving me back the same floats i'm putting in for some reason!
Avatar of Member_2_2394978

ASKER

Increase of points, because this is rather urgent now!
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

What are the values of

header.iNeuron

and

header.iWin

?
ASKER CERTIFIED SOLUTION
Avatar of Member_2_2394978
Member_2_2394978
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Member_2_2394978

ASKER

Oops, didn't see your message:  Sorry to mess you around.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>p.s.  What should I do with the question?

Just accept your own answer in which the above quote appears. Don't worry - glad you got it working
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo