java class that reads in files

java class that reads in files

I´ve got a lot of files in a folder with the ending .flow. Each file´s content show float variables which are coordinates. 2 values are 1 flow vector, that means: always the first value shows the x point, and every second shows the y point. Each .flow file looks like:
-0.776175
0.008398
-0.061091
0.019189
0.334752
0.185242

Now I want a java class (OpticalFlow) that reads in every .flow file:

public class OpticalFlow {



   // reads in the .flow file


   public float xComponent(int x, int y) {

     // returns the x-coordinate of the flow-vektor pixel(x,y)
   }

   
   public float yComponent(int x, int y) {

     // returns the y-coordinate of the flow-vektor pixel(x,y)

   }
}


I hope someone is able to help me and Thank you very much for your immediate answer!!
shed7Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sudhakar_koundinyaCommented:
int n=0;

try {
        BufferedReader in = new BufferedReader(new FileReader("infilename"));
        String str;
        while ((str = in.readLine()) != null) {
        if(n%2==0)
           x=Float.parseFloat(str);
         else
            y=Float.parseFloat(str);
          n++;
//handle x and y values as per ur requirements now    
        }
        in.close();
    } catch (IOException e) {
    }


Regards
Sudhakar
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
petmagdyCommented:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;


public class OpticalFlow {
    float[][] cordinates;
   
     public OpticalFlow()
     {
         readData();
     }
   
     public void readData()
     {
        try {
            BufferedInputStream in = new BufferedInputStream(new FileInputStream("c:\\tempo\\data.flow"));
           
            //read matrix from file
                byte[] data = new byte[in.available()];
                in.read(data);
                String strData = new String(data);
                String[] elements = strData.split("/s");
                   cordinates = new float[(elements.length/2)+1][2];
                   int j=0;

                   boolean isX = false;
                for(int i=0; i < elements.length; i++)
                {
                    System.out.println(elements[i]);
                    try
                    {
                    if(isX)
                    {
                        cordinates[j][0] = Integer.parseInt(elements[i]);
                        isX = false;
                    }
                    else
                    {
                           cordinates[j][0] = Integer.parseInt(elements[i]);
                        isX = false;
                        j++;
                    }
                    }
                    catch(NumberFormatException ne) {}
                   
                }

        } catch (Exception ex) {
            System.out.println("Error reading file");
            ex.printStackTrace();
        }
         
     }
     
   
    public static void main(String[] args) {
        OpticalFlow optFlow = new OpticalFlow();
    }
}

now u know what to do in ur xComponent, yComponent methods
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.