Foros del Web » Programación para mayores de 30 ;) » Java »

ayuda con grabar en un txt

Estas en el tema de ayuda con grabar en un txt en el foro de Java en Foros del Web. la duda es la siguiente en código que dejo mas abajo necesito grabar en un archivo txt la variable "total" que guarda todos los datos ...
  #1 (permalink)  
Antiguo 14/11/2007, 19:59
 
Fecha de Ingreso: noviembre-2007
Mensajes: 17
Antigüedad: 16 años, 5 meses
Puntos: 1
ayuda con grabar en un txt

la duda es la siguiente en código que dejo mas abajo necesito grabar en un archivo txt la variable "total" que guarda todos los datos que ingresaron el los diferentes TextField si me pueden ayudar con el codigo muchas gracias ya que llevo 3 dias en esto y no me sale nada

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class Ingresoc extends Frame
{
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();

Button btn1 = new Button("Grabar");
Button btn2 = new Button("Buscar");
Button btn3 = new Button("Salir");
TextField txt1 = new TextField(10);
TextField txt2 = new TextField(10);
TextField txt3 = new TextField(10);
TextField txt4 = new TextField(10);
TextField txt5 = new TextField(10);
TextField txt6 = new TextField(10);
TextField txt7 = new TextField(10);
TextField txt8 = new TextField(10);
TextField txt9 = new TextField(10);
Label lbl1 = new Label("Rut: ");
Label lbl2 = new Label("Nombres: ");
Label lbl3 = new Label("Apellido Paterno: ");
Label lbl4 = new Label("Apellido Materno: ");
Label lbl5 = new Label("Fecha de Nacimiento:");
Label lbl6 = new Label("Edad: ");
Label lbl7 = new Label("Dirección: ");
Label lbl8 = new Label("Teléfono: ");
Label lbl9 = new Label("Mail: ");



public Ingresoc()
{
super("Clientes");
setSize(300,320);
setLocation(300,300);
setResizable(false);

p2.add(lbl1,BorderLayout.WEST);
p2.add(txt1,BorderLayout.EAST);
p2.add(lbl2,BorderLayout.WEST);
p2.add(txt2,BorderLayout.EAST);
p2.add(lbl3,BorderLayout.WEST);
p2.add(txt3,BorderLayout.EAST);
p2.add(lbl4,BorderLayout.WEST);
p2.add(txt4,BorderLayout.EAST);
p2.add(lbl5,BorderLayout.WEST);
p2.add(txt5,BorderLayout.EAST);
p2.add(lbl6,BorderLayout.WEST);
p2.add(txt6,BorderLayout.EAST);
p2.add(lbl7,BorderLayout.WEST);
p2.add(txt7,BorderLayout.EAST);
p2.add(lbl8,BorderLayout.WEST);
p2.add(txt8,BorderLayout.EAST);
p2.add(lbl9,BorderLayout.WEST);
p2.add(txt9,BorderLayout.EAST);


p3.add(btn1,BorderLayout.SOUTH);
p3.add(btn2,BorderLayout.SOUTH);
p3.add(btn3,BorderLayout.SOUTH);

btn1.addActionListener(new oyente());
btn1.setActionCommand("Grabar");
btn2.addActionListener(new oyente2());
btn2.setActionCommand("Buscar");
btn3.addActionListener(new oyente3());
btn3.setActionCommand("Salir");

add(p1,BorderLayout.WEST);
add(p2,BorderLayout.CENTER);
add(p3,BorderLayout.SOUTH);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
System.exit(0);
}
});

show();

}
public class oyente implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
String Str = e.getActionCommand();
if (Str.compareTo("Grabar")==0)
{
int a=0;
//---------------------------------------------------------------------------------------------------
String Rut = new String(txt1.getText());
String Nomb = new String(txt2.getText());
String apepat = new String(txt3.getText());
String apemat = new String(txt4.getText());
String fchnac = new String(txt5.getText());
String edad = new String(txt6.getText());
String direcc = new String(txt7.getText());
String telefo = new String(txt8.getText());
String mail = new String(txt9.getText());
if(Rut.length()==10)
{
valida rut=new valida();
int r = rut.val(Rut);
if(r==50){
a = a + 1;
}else
{
JOptionPane.showMessageDialog (null,"Rut incorrecto ");
}
}else
{
JOptionPane.showMessageDialog (null,"Rut incorrecto ");
}

nombre nom=new nombre();
int n = nom.nombr(Nomb);
int m = Nomb.length();
if(n==5){
if(m > 2){
a = a + 1;
}else
{
JOptionPane.showMessageDialog (null,"El Nombre es incorrecto");
}
}else
{
JOptionPane.showMessageDialog (null,"El Nombre es incorrecto ");
}

nombre apell=new nombre();
int x = apell.nombr(apepat);
int k = apepat.length();
if(x==5){
if(k > 4){
a = a + 1;
}else
{
JOptionPane.showMessageDialog (null,"El Apellido paterno es incorrecto ");
}
}else
{
JOptionPane.showMessageDialog (null,"El Apellido paterno es incorrecto ");
}

nombre mater=new nombre();
int t = mater.nombr(apemat);
int q = apemat.length();
if(t==5){
if(q > 4){
a = a + 1;
}else
{
JOptionPane.showMessageDialog (null,"El Apellido Materno es incorrecto ");
}
}else
{
JOptionPane.showMessageDialog (null,"El Apellido Materno es incorrecto ");
}

int p;
char com;
if((p=fchnac.length()) > 7){
if(p == 8){
if((com = fchnac.charAt(2)) == '/'){
if((com = fchnac.charAt(5)) == '/'){
a = a + 1;
}
else{
JOptionPane.showMessageDialog (null,"La fecha es incorrecta xx/xx/xx 1");
}
}
else{
JOptionPane.showMessageDialog (null,"La fecha es incorrecta xx/xx/xx 2");
}
}
else{
JOptionPane.showMessageDialog (null,"La fecha es incorrecta xx/xx/xx 3");
}

}
else{
JOptionPane.showMessageDialog (null,"La fecha es incorrecta xx/xx/xx 4");
}


valida tele = new valida();
int te = tele.num(telefo);
if(te == 5){
a = a + 1;
}
else{
JOptionPane.showMessageDialog (null,"En Telefono : solo se aceptan números");
}

if(a == 6){
String total = new String(Rut+"/"+Nomb+"/"+apepat+"/"+apemat+"/"+fchnac+"/"+edad+"/"+direcc+"/"+telefo+"/"+mail);
graba g = new graba();
boolean res=g.cadena(total);

}
}
}
}
public class oyente2 implements ActionListener
{


public void actionPerformed (ActionEvent e)
{
String Str = e.getActionCommand();
if (Str.compareTo("Buscar")==0)
{
JOptionPane.showMessageDialog (null,"");
}
}
}
public class oyente3 implements ActionListener
{


public void actionPerformed (ActionEvent e)
{
String Str = e.getActionCommand();
if (Str.compareTo("Salir")==0)
{
dispose();
}
}
}
  #2 (permalink)  
Antiguo 15/11/2007, 02:43
 
Fecha de Ingreso: septiembre-2007
Mensajes: 173
Antigüedad: 16 años, 7 meses
Puntos: 0
Re: ayuda con grabar en un txt

FileWriter fichero = null;
String datos = "Texto que voy a escribir en el fichero";
try
{
// Creas la instancia del fichero
fichero = new FileWriter("c:\\midirectorio\\mifichero.txt");
// El carácter \r\n es el que hace que salte una linia en el fichero después de escribir
fichero.write(datos + "\r\n");
// Cierras el fichero
fichero.close();
}
catch(IOException ioe)
{
// Aqui pones lo que quieres que haga cuando se da una excepción de escritura
}

Pare recuperar los datos del JTextField, se usa el método getText().

Saludos.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 11:09.