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();
}
}
directorio + archivo
, debieras tener algo como:directorio + File.separator + archivo