1

tengo ciertas dudas en lo que se refiere a crear directorios y archivos.

Consigo crear la carpeta con un nombre cualquiera que se ingrese por teclado, pero no consigo crear el archivo.

Preferiría una vez resuelta esta duda en concreto tratar de hacer el ejercicio antes de postear las diferentes dudas y si por algún motivo no avanzo editar este post, gracias de antemano.

Editado: Entiendo que para nuevas dudas sobre el mismo ejercicio lo mejor es formular otra pregunta.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Date;
import java.util.Scanner;


public class Principal {


    public static void main(String[] args) throws IOException {
         String directorio = null;
         String archivo = null;
         int numero=0;
         int contador = 0;


         Cliente cliente=new Cliente();


         Scanner teclado = new Scanner(System.in);
         System.out.println("Ingrese el directorio");
         directorio = teclado.nextLine();
         boolean exito = (new File(directorio)).mkdir();

         System.out.println("Ingrese el nombre del archivo");
         archivo=teclado.nextLine();
         File fichero = new File(directorio, archivo);
         fichero.createNewFile();
         System.out.println("Numero de clientes que se van a guardar en el archivo");
         numero=teclado.nextInt();
             while(contador <numero){
                 contador++;
                 cliente.datos();

           }  

    }

}

Esta es la clase.

import java.io.Serializable;
import java.util.Scanner;

public class Cliente implements Serializable {
    private String nombre;
    private int edad;
    private int telefono;
    private String nif;
    private String direccion;


    public Cliente(){

    }

    public Cliente(String nombre, int edad, int telefono, String nif, String direccion){
        this.nombre=nombre;
        this.edad=edad;
        this.telefono=telefono;
        this.nif=nif;
        this.direccion=direccion;

    }

    /**
     * @return the nombre
     */
    public String getNombre() {
        return nombre;
    }

    /**
     * @param nombre the nombre to set
     */
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    /**
     * @return the edad
     */
    public int getEdad() {
        return edad;
    }

    /**
     * @param edad the edad to set
     */
    public void setEdad(int edad) {
        this.edad = edad;
    }

    /**
     * @return the telefono
     */
    public int getTelefono() {
        return telefono;
    }

    /**
     * @param telefono the telefono to set
     */
    public void setTelefono(int telefono) {
        this.telefono = telefono;
    }

    /**
     * @return the nif
     */
    public String getNif() {
        return nif;
    }

    /**
     * @param nif the nif to set
     */
    public void setNif(String nif) {
        this.nif = nif;
    }

    /**
     * @return the direccion
     */
    public String getDireccion() {
        return direccion;
    }

    /**
     * @param direccion the direccion to set
     */
    public void setDireccion(String direccion) {
        this.direccion = direccion;
    }


         public void datos(){
         Scanner teclado = new Scanner(System.in);
         System.out.println("Ingrese el nombre");
         this.nombre=teclado.nextLine();
         System.out.println("Ingrese la edad");
         this.edad=teclado.nextInt();
         System.out.println("Ingrese el telefono");
         this.telefono=teclado.nextInt();
         System.out.println("Ingrese el NIF");
         this.nif=teclado.nextLine();
         System.out.println("Ingrese la direccion");
         this.direccion=teclado.nextLine();
         }

}
1
  • A primera vista, construyes mal el nombre de la ruta completa del archivo. En lugar de directorio + archivo, debieras tener algo como: directorio + File.separator + archivo
    – jachguate
    el 4 abr. 2019 a las 21:46

2 respuestas 2

0

Creo que donde estás concatenando el nombre del archivo (directorio+archivo) es donde lo estás haciendo incorrecto. Quizás sería directorio +"/"+ archivo. Pero yo usaría mejor:

     File fichero = new File(directorio, archivo); // coma en lugar de +

Así creas el archivo bajo ese directorio.

0
0

Otra opción es utilizar las nuevas clases de java "New IO" o mejor conocidas como NIO que están disponibles a partir del JDK7:

import java.nio.file.Path;
import java.nio.file.Files;

    Path newFilePath = Paths.get("tu_directorio/newFile_jdk7.txt");
    Files.createFile("newFilePath);

Salu2!

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.