You doubt about writing a file at random

0
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.RandomAccessFile;

    public class Jugadores {

    public static void main(String[] args) throws IOException {
    //declaramos objeto jugador
    Jugadores jugador = new Jugadores();
    //apertura del fichero
    RandomAccessEjercicio Apertura = new RandomAccessEjercicio("C:\Users\FER\Downloads\jugadores.txt","rw");
    //creamos el fichero
    Apertura.crearFichero();
    //declaramos el objeto
    RandomAccessFile objeto2;
    //conseguimos el objeto random a traves del metodo get object
    objeto2=Apertura.getObj();
    //escribimos el array
    jugador.escribirArrayFichero(objeto2);

}

public void escribirArrayFichero(RandomAccessFile param) throws IOException {
    String[] jugadores = {"jugador 1 ","jugador 2","jugador 3","jugador 4","jugador 5"};
    String[] apellidos = {"Garcia","nuñez","Gomez","Nuñez","Rios"};
    String[] equipo = {"Real Madrid","Atletic","Barcelona ","Betis","Sevilla"};
    String[] posicion = {"central","lateral","delantero","portero","Extremo"};
    int[] edad = {18,21,19,20,22};
    int i = 0;
    param.writeBytes("NOMBRE");
    param.writeBytes("\r\n");
    for(i=0;i<5;i++) {;
    param.writeBytes("\r\n"+jugadores[i]);
    }
    param.writeBytes("\n\r");
    param.writeBytes("\n"+"APELLIDOS");
    for(i=0;i<5;i++) {;
    param.writeBytes("\r\n"+apellidos[i]);
    }
    param.writeBytes("EQUIPO");
    param.writeBytes("\r\n");
    for(i=0;i<5;i++) {;
    param.writeBytes("\r\n"+equipo[i]);
    }
    param.writeBytes("\n\r");
    param.writeBytes("\n"+"APELLIDOS");
    for(i=0;i<5;i++) {;
    param.writeBytes("\r\n"+posicion[i]);
    }
    param.writeBytes("\n\r");
    param.writeBytes("\n"+"EDAD");
    for(i=0;i<5;i++) {;
    param.writeBytes("\r\n"+edad[i]);
    }

}

}

I do not write the fields correctly in the file, example:

NOMBRE APELLIDOS EQUIPO POSICION EDAD
 x        x        x       x       x
 x        x        x       x       x
 x        x        x       x       x
 x        x        x       x       x
 x        x        x       x       x

I've tried it with the method seek but I still write horizontally, I know it's a matrix and I should do an array of two positions but I do not know how to actually do it

    
asked by J.newbie 15.11.2018 в 11:20
source

1 answer

0

I think it could be done with a for, since the data you want to show in a row as you indicate.

public void escribirArrayFichero(RandomAccessFile param) throws IOException {
    String[] jugadores = {"jugador 1 ","jugador 2","jugador 3","jugador 4","jugador 5"};
    String[] apellidos = {"Garcia","nuñez","Gomez","Nuñez","Rios"};
    String[] equipo = {"Real Madrid","Atletic","Barcelona ","Betis","Sevilla"};
    String[] posicion = {"central","lateral","delantero","portero","Extremo"};
    int[] edad = {18,21,19,20,22};
    int i = 0;

    for (i = 0; i < 5; i++)
    {
        param.writeBytes(jugadores[i] + "\t" + apellidos[i] + "\t" + equipo[i] + "\t" + posicion[i] + "\t" + edad[i] +"\r\n");    
    }
 }

I hope it works for you, I've tried it and I show you the result by console:

jugador 1   Garcia  Real Madrid     central 18
jugador 2   nu?ez   Atletic     lateral 21
jugador 3   Gomez   Barcelona       delantero   19
jugador 4   Nu?ez   Betis       portero 20
jugador 5   Rios    Sevilla     Extremo 22
    
answered by 15.11.2018 / 12:32
source