Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

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!!
0
shed7
Asked:
shed7
1 Solution
 
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
 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now