如何在java中将文件拆分为n个部分?

时间:2016-06-23 21:55:57

标签: java file io

我在java中完成了一个应用程序,它对文件执行相同的操作。实际上我的应用程序只是为文件执行阈值方案(n,k)。这是完美的工作,但我能够理解它,因为我使用System.out.println()方法。我真正想要做的只是真正拆分文件。例如,如果我有文件myfile.txt并且我使用阈值方案(7,3)我真的想将我的文件分成7个部分,如myfile.txt1,myfile.txt2等等。到目前为止我真正做的是:

 public  int[][] calculateThresholdScheme(byte[] secret, int n, int k, RndGeneratorAllAlgorithm rnd,FastFiniteField256 galoa) {

                /* secret is the content of the file in byte
                 * n is in how many parts we will share the file
                 * k means that from n parts of shares we need k parts to take file back
                 * rnd is an algorithm which generate random numbers
                 * galoa is a finite field 256 which perform mathematical operation
              */


   if (secret == null)
       throw new IllegalArgumentException("null secret"); 
   // throws exceptions if parameters of our method do not meet the conditions of SSS

   int m = secret.length;

   if (m == 0) // the secret length must  not be 0
       throw new IllegalArgumentException("invalid secret length: 0");


   if (n < 1) // we should have at least one share
       throw new IllegalArgumentException("not enought shares: " + n);


   if (n > MaxShares) // we can not have integer shares greater than 256 
       throw new IllegalArgumentException("too many shares: " + n + "(gt" + MaxShares + ")");


   if (k > n) // threshold can not be bigger than shares
       throw new IllegalArgumentException("threshold > shares: " + k + " > " + n);

       /**We must take a two dimensional vector of share which will contain all pairs of share for each byte of file
         for example (1,f(1)) , (2,f(2)) , (3,f(3)) where 1,2,3 are the value of shares */


   int[][] share = new int[n][m + 1]; // this one will only create a two dimensional array

   /*
   System.out.println("It has " + n + " rows , and " +m + " columns");
   System.out.println("The number of rows means in how much shares we will share each byte");
    System.out.println("The number of columns means how bytes our file has");
   */

       for (int j = 0; j < n; j++) 
       share[j][0] =  (j + 1);


      for (int j=0;j<m;j++) { //this will take bytes of files one by one (fix column)

       // each byte of the file will be the secret
          coeff=secret[j]; 


       // create an array of size k where k is the number of random coefficients 
            allCoefficients=new int[k];


            // call generateCoefficents method and assign its content to the array allCoefficients
          allCoefficients=rnd.supply(k,coeff);

    //  System.out.println(Arrays.toString(allCoefficients));

           for (int i=0;i<n;i++) {

   share [i][j+1]= (byte)galoa.hornerEvaluation(share[i][0],allCoefficients);


          } // close nested loop which goes through rows


       } // close outer loop which goes through columns

     System.out.println(Arrays.deepToString("share");
     return share;
   }

这确实很好用。例如,如果我有一个文件,请说只是为了演示11字节。对于第一个字节,我们将为第二个字节7值提供7个值,依此类推。现在我真的很挣扎如何真正使用这种分割文件的方法,以便将我的文件分成七个部分。换句话说,我想要这个方法,我在这里做一些类似于java api中的split()方法。

提前致谢

0 个答案:

没有答案