0

Tengo una pequeña duda y es que no se como hacer para que cuando añadas información en un fichero txt pues salga una debajo de la otra , Si alguien sabe como hacerlo estaría muy guay, Estuve buscando por mi cuenta pero no me sale , lo he intentado varias veces pero tampoco me sale. El resultado de que me sale es:Josep;20;SantsMaria;20;Sants , es decir todo junto

 public void anadirClientes(){
        Scanner input=new Scanner(System.in);
    BufferedWriter bw = null;
    FileWriter fw = null;

    try {
        String name;
        String edad;
        String barrio;
        System.out.println("Introduce nombre");
        name=input.nextLine();
        System.out.println("Introduce edad");
        edad=input.nextLine();
        System.out.println("Introduce barrio");
        barrio=input.nextLine();
        File file = new File("clientes.txt");
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
        fw = new FileWriter(file.getAbsoluteFile(), true);
        bw = new BufferedWriter(fw);
        bw.write(name);
        bw.write(";");
        bw.write(edad);
        bw.write(";");
        bw.write(barrio);
        bw.write("\n");
        System.out.println("información agregada!");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (bw != null)
                bw.close();
            if (fw != null)
                fw.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
2

1 respuesta 1

1

Si mal no entiendo tu pregunta, lo que quieres lograr son saltos de linea.

puedes usar el método newLine() del BufferedWriter, algo así:

bw.newLine();
1
  • Okay, Muchas Gracias me funcionó! el 2 may. 2020 a las 9:02

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.