package DDMT.Nodes;

import java.io.*;
import java.lang.*;
import java.util.*;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.rmi.RemoteException;


class FileOperations 
{
    FileInputStream fstream;
    static String fileData;
	
    public static Vector ReadFile(String filename) 
    {
        String t;
        Vector SiteXData = new Vector();

        try
        {
            File f ;
            if (filename!=null && filename.length()!=0)
            {
              f = new java.io.File(filename);
            }
            else
            {
              System.out.println("file name incorrect!");
              return SiteXData;
            }

        // read file
            String line;
            StringTokenizer StrTok = null;
            int NumRows = 0; // total number of rows
            int NumCols = 0; // total number of columns
            int tmpNumCols = 0;

            // open file
            java.io.BufferedReader in = new java.io.BufferedReader(new java.io.FileReader(f));
            // get the number of columns
            line = in.readLine();
            if (line == null)
            {
                System.out.println("empty file!");
                return SiteXData;
            }
            else
            {
                StrTok = new StringTokenizer(line," \t\n\r\f,");
                NumCols = StrTok.countTokens();
            //    System.out.print(NumCols);
            }
            in.close();

            // initial a data point array
            double [] DataArray;
            in = new java.io.BufferedReader(new java.io.FileReader(f));

            //now read file to pick up values
            while (null != (line = in.readLine())) 
            {
                NumRows ++ ;
                StrTok = new StringTokenizer(line," \t\n\r\f,");
                tmpNumCols = StrTok.countTokens();
                if (tmpNumCols != NumCols) 
                {
                    System.out.println("inconsistent columns in row "+NumRows+" .");
                    SiteXData.clear();
                    return SiteXData;
                }

                DataArray = new double [NumCols];
                for(int i=0;i < NumCols;i++) 
                    DataArray[i] = (Double.valueOf(StrTok.nextToken())).doubleValue();
             
                Point dp = new Point(DataArray,-1);
                SiteXData.add(dp);
            }
            
            in.close();
        }
        catch (Exception e) 
        {
        e.printStackTrace();
        }
        
        return SiteXData;
    }

    public static void WriteFile(String filename,Vector PointList)
    {
        String fileData = "";
        for (int i=0;i<PointList.size();i++)
        {
            Point dp = (Point)PointList.elementAt(i);
            for (int j=0;j<dp.d;j++)
                fileData = fileData.concat(Double.toString(dp.x[j])+" ");
            fileData = fileData.concat("\n");
        //    System.out.println("Finally :" +dp.x+" "+dp.y+" "+dp.membership);
        }
	//	System.out.println("FileData : "+fileData);
	try 
	{
            BufferedWriter out = new BufferedWriter(new FileWriter(filename));
                    
            out.write(fileData);
            out.close();
        }
	catch (IOException e) 
	{
		System.out.println("Writing exception");
        }
    }
	
    
    public static void WriteFile(String filename,String fileData)
    {
	try 
	{
            BufferedWriter out = new BufferedWriter(new FileWriter(filename));
                    
            out.write(fileData);
            out.close();
        }
	catch (IOException e) 
	{
		System.out.println("Writing exception");
        }
    }
	
//    public static void main(String[] argc)
//    {
//            System.out.println("------------Reading file----------");
//            System.out.println("Reading from file test.txt");
//            String fname = new String("test.txt");
//            ReadFile(fname);
//            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//            System.out.println("------------Writing file----------");
//            System.out.println("Enter the filename (with full path)");
//            String filename = null;
//
//            try 
//            {
//                    filename = br.readLine();
//            } catch (IOException e1) {
//                    // TODO Auto-generated catch block
//                    e1.printStackTrace();
//            }
//
//            System.out.println("Enter the contents of the file");
//
//        String fileData=null;
//            try 
//            {
//                    fileData = br.readLine();
//            } catch (IOException e) 
//            {
//                    // TODO Auto-generated catch block
//                    e.printStackTrace();
//            }
//    //	WriteFile(filename,fileData);
//    }

}

