que necesito hacer que cuando ingreso unos datos me los valla guardando en un fichero en este caso un txt lo bonito es que cuendo voy ingresando los datos me los va escribiendo en el fichero, lo feo es que cuendo ingreso otro listado de datos me los va escribiendo en la smismas lineas donde estaban los datos del listado anterior oce ala cosa es que tengo que ir ingresdando clientes y los datos los va wardando en un fichero pero quiero hacer que se vallan guardando en blokeas acia abajo pero no puedo hacerlo miren aka les dejo los codigos que llevo
este es la clase para escribir
Código:
import java.io.*;
import java.util.Date;
import java.text.DateFormat;
public class EscrituraContacto {
private FileWriter writer;
private BufferedWriter buffer;
private PrintWriter output;
public void abrir() throws IOException {
try {
writer = new FileWriter( "clientes.txt" );
buffer = new BufferedWriter(writer);
output = new PrintWriter(writer);
}
catch (SecurityException securityException) {
System.err.println("No tiene permiso para escribir en el fichero");
throw securityException;
}
catch (FileNotFoundException fileException) {
System.err.println("Error al crear el fichero" );
throw fileException;
}
}
public void escribir (String a) {
if (a!=null){
output.println(a);
}
}
public void cerrar()throws IOException {
if ( output != null )
output.close();
}
}
import java.util.Date;
import java.text.DateFormat;
public class EscrituraContacto {
private FileWriter writer;
private BufferedWriter buffer;
private PrintWriter output;
public void abrir() throws IOException {
try {
writer = new FileWriter( "clientes.txt" );
buffer = new BufferedWriter(writer);
output = new PrintWriter(writer);
}
catch (SecurityException securityException) {
System.err.println("No tiene permiso para escribir en el fichero");
throw securityException;
}
catch (FileNotFoundException fileException) {
System.err.println("Error al crear el fichero" );
throw fileException;
}
}
public void escribir (String a) {
if (a!=null){
output.println(a);
}
}
public void cerrar()throws IOException {
if ( output != null )
output.close();
}
}
esta es de donde llamo
Código:
import java.io.*;
public class Taller{
public static Listas listas = new Listas();
public static BufferedReader teclado = new BufferedReader (new InputStreamReader(System.in));
public static Taller obj = new Taller();
public static byte opc = 0;
public static EscrituraContacto cliente = new EscrituraContacto();
public static void main(String[] args) throws IOException{
obj.menu();
}
public void menu()throws IOException{
System.out.println( "\t\t\t\t MENU PRINCIPAL\n\n");
System.out.println( "\t\t\t 1.- Ingresar Cliente\n");
System.out.println( "\t\t\t 2.- Buscar Cliente \n ");
System.out.println( "\t\t\t 3.- Agregar Cliente\n ");
System.out.println( "\t\t\t 4.- Modificar Cliente\n ");
System.out.println( "\t\t\t 5.- Salir \n ");
System.out.print("\nIngrese opcion : ");
try {
opc = Byte.parseByte (teclado.readLine());
}
catch (Exception e) {
System.out.println("\n\nIngrese solo Numeros");
obj.menu();
}
switch (opc){
case 1 : obj.menu();
break;
case 2 : obj.menu();
break;
case 3 :obj.agregarCliente();
obj.menu();
break;
case 4 : obj.menu();
break;
case 5 : System.exit(0);
break;
default :System.out.println("\nINGRESE UNA OPCION VALIDA");
obj.menu();
break;
}
}
public void agregarCliente()throws IOException{
String nombre, apellido,direccion,telefono,rut;
System.out.println("Ingrese Nombre");
nombre = teclado.readLine();
System.out.println("Ingrese Apellido");
apellido = teclado.readLine();
System.out.println("Ingrese rut");
rut = teclado.readLine();
System.out.println("Ingrese Direccion");
direccion = teclado.readLine();
System.out.println("Ingresar Telefono");
telefono = teclado.readLine();
listas.addCliente(new Cliente(nombre, apellido, rut,telefono, direccion));
cliente.abrir();
cliente.escribir(nombre);
cliente.escribir(apellido);
cliente.escribir(rut);
cliente.escribir(direccion);
cliente.escribir(telefono);
cliente.cerrar();
}
}
public class Taller{
public static Listas listas = new Listas();
public static BufferedReader teclado = new BufferedReader (new InputStreamReader(System.in));
public static Taller obj = new Taller();
public static byte opc = 0;
public static EscrituraContacto cliente = new EscrituraContacto();
public static void main(String[] args) throws IOException{
obj.menu();
}
public void menu()throws IOException{
System.out.println( "\t\t\t\t MENU PRINCIPAL\n\n");
System.out.println( "\t\t\t 1.- Ingresar Cliente\n");
System.out.println( "\t\t\t 2.- Buscar Cliente \n ");
System.out.println( "\t\t\t 3.- Agregar Cliente\n ");
System.out.println( "\t\t\t 4.- Modificar Cliente\n ");
System.out.println( "\t\t\t 5.- Salir \n ");
System.out.print("\nIngrese opcion : ");
try {
opc = Byte.parseByte (teclado.readLine());
}
catch (Exception e) {
System.out.println("\n\nIngrese solo Numeros");
obj.menu();
}
switch (opc){
case 1 : obj.menu();
break;
case 2 : obj.menu();
break;
case 3 :obj.agregarCliente();
obj.menu();
break;
case 4 : obj.menu();
break;
case 5 : System.exit(0);
break;
default :System.out.println("\nINGRESE UNA OPCION VALIDA");
obj.menu();
break;
}
}
public void agregarCliente()throws IOException{
String nombre, apellido,direccion,telefono,rut;
System.out.println("Ingrese Nombre");
nombre = teclado.readLine();
System.out.println("Ingrese Apellido");
apellido = teclado.readLine();
System.out.println("Ingrese rut");
rut = teclado.readLine();
System.out.println("Ingrese Direccion");
direccion = teclado.readLine();
System.out.println("Ingresar Telefono");
telefono = teclado.readLine();
listas.addCliente(new Cliente(nombre, apellido, rut,telefono, direccion));
cliente.abrir();
cliente.escribir(nombre);
cliente.escribir(apellido);
cliente.escribir(rut);
cliente.escribir(direccion);
cliente.escribir(telefono);
cliente.cerrar();
}
}
bueno estoy en pañales con esta cosa de java asi que nos e mucho asi que si encuentran que el codigo es algo precario nose rian XD