04 agosto 2011

#{ Java: Escritura de archivos }

0 comentarios
import java.io.*;
import java.lang.*;

class ArchivoEscribir
{

    public static void main(String [] args){

    try{
        String linea_arch= "texto a escribir en archivo";
        File f=new File("Archivo.txt");
        //InputStreamReader isr= new InputStreamReader(System.in);
        //BufferedReader linea= new BufferedReader(isr);

        //System.out.println("DIGITE NOMBRE: \t");
        //String linea_arch=linea.readLine();

        FileWriter linea_tx= new FileWriter(f,false);
        linea_tx.write(linea_arch);

        linea_tx.close();

    }catch(IOException e){}
    }
}

#{ IOThorium }

0 comentarios
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;

/**
 *
 * @author 
 */
public class Mail {

    private int tipoMje;
    private int status;
    private String de;
    private String para;
    private String cCopia;
    private String asunto;
    private String mensaje;
    private int tipoForm;
    private int ioStatus;


    /**
     * Constructor de la Clase.
     */
    public Mail(){
    }

    public void setTipoMje(int tipoMje){
        this.tipoMje = tipoMje;
    }

    public int getTipoMje(){
        return tipoMje;
    }

    /**
     * @return the de
     */
    public String getDe() {
        return de;
    }

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

    public void setPara(String para){
        this.para = para;
    }

    public String getPara(){
        return para;
    }

    public void setCCopia(String cCopia){
        this.cCopia = cCopia;
    }

    public String getCCopia(){
        return cCopia;
    }

    public void setAsunto(String asunto){
        this.asunto = asunto;
    }

    public String getAsunto(){
        return asunto;
    }

    public void setMensaje(String mensaje){
        this.mensaje = mensaje;
    }

    public String getMensaje(){
        return mensaje;
    }   
    
    public int getStatus() {
        return status;
    }

    /**
     * Establece el estado del mensaje: 

* 1 -> Recibido * 2 -> Enviado * 3 -> Borrador * 4 -> Eliminado * * @param status */ public void setStatus(int status) { this.status = status; } /** * @return the ioStatus */ public int getIoStatus() { return ioStatus; } /** * Establece el estado del mensaje:

* 0: * -Enviado: En cola de envio * -Recibido: No leido * * @param ioStatus the ioStatus to set */ public void setIoStatus(int ioStatus) { this.ioStatus = ioStatus; } /** * @return the tipoForm */ public int getTipoForm() { return tipoForm; } /** * @param tipoForm the tipoForm to set */ public void setTipoForm(int tipoForm) { this.tipoForm = tipoForm; } public void verDatos(){ System.out.print("Para -> "+para); System.out.print("\nCc -> "+cCopia); System.out.print("\nAsunto -> "+asunto); System.out.print("\nMensaje -> "+mensaje); Connection conn = null; Statement stmt = null; ResultSet rs = null; DbConnection conex = new DbConnection(); //CustomDefaultTableModel modelo = new CustomDefaultTableModel(); conex.openConnection(); conn = conex.getDbConnection(); try { stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT name FROM species WHERE category='"+38+"' "); while(rs.next()) { Object[] fila = new Object[1]; for (int i=0;i<1;i++){ fila[i] = rs.getObject(i+1); JOptionPane.showMessageDialog(null,"-> "+rs.getObject(i+1).toString()); } //modelo.addRow(fila); } stmt.close(); conex.closeConnection(); }catch(SQLException ex) { System.out.println(ex); JOptionPane.showMessageDialog(null,"Error"+ex.getMessage()); } //return modelo; } /** * Guarda un mensaje recibido, enviado, guardardo o eliminado y * se almacena en la Base de Datos. * Este puede ser del tipo Correo o Formulario. * * @throws SQLException En caso de fallar la inserción en la BD. */ public void saveMail(){ Connection conn = null; Statement stmt = null; ResultSet rs = null; DbConnection conex = new DbConnection(); java.util.Date fechas = new java.util.Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String format = dateFormat.format(fechas); String rstatus = "NO"; int mthID = 0; conex.openConnection(); conn = conex.getDbConnection(); try { stmt = conn.createStatement(); stmt.executeUpdate("INSERT INTO messages VALUES("+ null +","+ tipoMje +","+ status +",'"+ de +"','"+ para +"','"+ cCopia +"','"+ asunto +"','"+ mensaje +"','"+ format +"',"+ ioStatus +",'"+ rstatus +"',"+ mthID +")"); stmt.close(); stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT last_insert_rowid()"); //Insert en la tabla user_has_messages triggerSaveMailRecord(rs.getInt(1)); conex.closeConnection(); rs.close(); }catch(SQLException ex) { System.out.println(ex); JOptionPane.showMessageDialog(null,"Error"+ex.getMessage()); } } /** *Inserta en la tabla dedicada a guardar los registros de la asociacion entre * los mensajes y los usuarios. * Es decir, los mensajes que envia cada usuario.

* Este metodo se dispara inmediatamente despues de enviarse un mensaje. * * @param idLastInsertded El id del mensaje recien insertado */ public void triggerSaveMailRecord(int idLastInsertded){ Connection conn = null; Statement stmt = null; DbConnection conex = new DbConnection(); conex.openConnection(); conn = conex.getDbConnection(); try { stmt = conn.createStatement(); //Para extraer el usuario del String completo (xxx.tid@thorium.cunlogan.cl) StringTokenizer tokens = new StringTokenizer(de,"."); String usname = tokens.nextElement().toString(); stmt.executeUpdate("INSERT INTO user_has_messages VALUES('"+ usname +"',"+ idLastInsertded +")"); stmt.close(); conex.closeConnection(); }catch(SQLException ex) { System.out.println(ex); JOptionPane.showMessageDialog(null,"Error: "+ex.getMessage()); } return; } /** *Lista los mensajes almacenados del estado y tipo seleccionado * [recibido | enviado | borrador | eliminado] * * @param st El estado de guardado del mensaje. * @param typ El tipo de mensaje: Correo o Forms. * @return DefaultTableModel * @throws SQLException En caso de fallar la inserción en la BD. */ public CustomDefaultTableModel loadMailList(int st, int typ){ //Se arma el tipo de modelo a retornar. CustomDefaultTableModel modelo = new CustomDefaultTableModel(); //Se arma la grilla de correos dinamicamente segun estado y tipo del mensaje. modelo.addColumn("Id"); modelo.addColumn("Leído"); if(st == 1 || st == 4 || st == 0 || st == 5){ modelo.addColumn("De"); } if (st == 2 || st == 3 || st == 4){ modelo.addColumn("Para"); } modelo.addColumn("Asunto"); /*if(typ == 2){ modelo.addColumn("Tipo"); }*/ modelo.addColumn("Fecha"); Connection conn = null; Statement stmt = null; ResultSet rs = null; DbConnection conex = new DbConnection(); conex.openConnection(); conn = conex.getDbConnection(); int max = 0; try { stmt = conn.createStatement(); if(typ == 1){ max = 5; switch(st){ case 1: rs = stmt.executeQuery("SELECT id, read_status, from_em, subject, creation_date FROM messages WHERE status="+st+" AND type="+typ+" ORDER BY creation_date DESC"); break; case 2: rs = stmt.executeQuery("SELECT id, read_status, to_em, subject, creation_date FROM messages WHERE status="+st+" AND type="+typ+" ORDER BY creation_date DESC"); break; case 3: rs = stmt.executeQuery("SELECT id, read_status, to_em, subject, creation_date FROM messages WHERE status="+st+" AND type="+typ+" ORDER BY creation_date DESC"); break; case 4: max = 6; rs = stmt.executeQuery("SELECT id, read_status, from_em, to_em, subject, creation_date FROM messages WHERE status="+st+" AND type="+typ+" ORDER BY creation_date DESC"); break; default: rs = stmt.executeQuery("SELECT id, read_status, from_em, subject, creation_date FROM messages WHERE type="+typ+" ORDER BY creation_date DESC"); break; } }else{ max = 4; switch(st){ case 1: rs = stmt.executeQuery("SELECT m.id, m.from_em, m.subject, m.creation_date FROM messages m WHERE m.status="+st+" AND m.type="+typ+" "); break; case 2: rs = stmt.executeQuery("SELECT m.id, m.to_em, m.subject, m.creation_date FROM messages m WHERE m.status="+st+" AND m.type="+typ+" "); break; case 3: rs = stmt.executeQuery("SELECT m.id, m.to_em, m.subject, m.creation_date FROM messages m WHERE m.status="+st+" AND m.type="+typ+" "); break; case 4: max =5; rs = stmt.executeQuery("SELECT m.id, m.from_em, m.to_em, m.subject, m.creation_date FROM messages m WHERE m.status="+st+" AND m.type="+typ+" "); break; default: rs = stmt.executeQuery("SELECT m.id, m.from_em, m.subject, m.creation_date FROM messages m WHERE m.type="+typ+" "); break; } } while(rs.next()) { Object[] fila = new Object[max]; for (int i=0;i

 
Buffer de Código | © Design by DheTemplate.com and Theme 2 Blog

Find more free Blogger templates at DheTemplate.com - Daily Updates Free Blogger Templates