Quiero dividir un archivo que contiene bigintegers generados de forma aleatoria en 2 partes iguales.
Aquí está generando un archivo con números enteros grandes aleatorios
public void generateRandomBigInteger(){
if(file.length() > 0){
System.out.println("Sobreescriptura de fitxer no permesa!\n");
}
else {
//try-with-resources per asegurar el tancament del stream
try {
int tamany = 10000;
FileOutputStream fos = new FileOutputStream(file, true);
DataOutputStream dos = new DataOutputStream(fos);
for (int i = 0; i < tamany; i++){
BigIntegerIO.writeBigInteger(new NumberGenerator().generateBigInteger(), dos);
}
} catch (Exception e) {
System.out.println(e);
}
System.out.println("Numeros aleatoris generats correctament!!\n");
}
}
Tengo este método para dividir el archivo, pero no es la forma correcta.
public void splitFile() throws IOException {
//Definir el tamany de les noves parts (tamany del file numeros.txt / 2)
long fileSize = file.length();// 166,03 kB
double sizeOfFiles = fileSize/2;// 83,01kB
byte[] buffer = new byte[(int) sizeOfFiles +1];
//Obtendre el nome de file sense ".txt"
String fileName = file.getName();
//String fileName = name.replaceFirst("[.][^.]+$", "");
//Nombrar les parts 1, 2, ...
int partCounter = 1;
int cantitatBytes;
//try-with-resources per asegurar el tancament del stream
try{
FileInputStream fis = new FileInputStream(file);
DataInputStream bis = new DataInputStream(fis);
while((cantitatBytes = bis.read(buffer)) > 0){
//escriviu cada tros de dades en un fitxer separat amb un nombre diferent en nom
String nomPartFitxer = String.format ("%s.%01d", fileName, partCounter++);
File newFile = new File(nomPartFitxer);
FileOutputStream fos = new FileOutputStream(newFile);
//try-with-resources per asegurar el tancament del stream
try(DataOutputStream dos = new DataOutputStream(fos)){
dos.write(buffer, 0, cantitatBytes);
}
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("");
}
El método generate Random BigInteger () genera un archivo con 10.000 bigintegers