0

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

3
  • 1
    No estás haciendo un try-with-resources en el primer código, ese comentario es incorrecto el 27 oct. 2021 a las 12:15
  • @PabloLozano Yo pensaba que si! lo quito entonces. el 27 oct. 2021 a las 14:18
  • @PabloLozano y en el segundo código lo usa en el try anidado (que no tiene catch) pero no en el try-catch "exterior".
    – Alfabravo
    el 27 oct. 2021 a las 14:48

0

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

Examina otras preguntas con la etiqueta o formula tu propia pregunta.