package DDMT.Nodes;
import java.util.Vector;
import java.io.File;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class DistributeUmassFiles 
{
    int numPeers = 2000;
    String[] data = new String[numPeers];
    
    public DistributeUmassFiles() 
    {      
        try
        {
            for (int i=0;i<data.length;i++)
            {
                data[i] = "";
            }
            //first read the file one line at a time
            String ifilename = "D:\\P2PUMass\\2000User1000BandsID.txt";

            File file = new File(ifilename);
            byte buffer[] = new byte[(int)file.length()];
            BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
            input.read(buffer,0,buffer.length);
            input.close();					 
            String fileData = new String(buffer);
            String[] lines = fileData.split("\n");          

            for (int i=0;i<lines.length;i++)
            {
                System.out.println("Line: " +i);
                int peerid = Integer.parseInt((lines[i].split(" ")[1]).trim());
                data[peerid-1] = data[peerid-1].concat(lines[i])+"\n";
            }
            for (int i=0;i<this.numPeers;i++)
            {
                System.out.println("Peer : "+i);
                this.WriteFile("D:\\P2PUmass\\Datasets\\"+i+".txt", this.data[i]);
            }
        }
        catch (IOException e)
        {
            System.out.println(e);
        }
    }
    
    public  void WriteFile(String filename,String x) 
    {
        try 
        {
            BufferedWriter out = new BufferedWriter(new FileWriter(filename));
            out.write(x);
            out.close();
        }
        catch (IOException e) 
        {
            System.out.println("Writing exception");
        }
    }
    
    public static void main(String[] args) 
    {
        DistributeUmassFiles ds = new DistributeUmassFiles();
    }
    
}
