segunda-feira, 30 de maio de 2011

Aula 14 - 31.05.2011

Trabalhando com Exceptions em Java: 
Trabalhando com Datas em Java:
Trabalhando com Arquivos em Java:
Trabalhando com XML em Java:

import java.io.*;
import java.beans.XMLEncoder;
import java.beans.XMLDecoder;

public class XMLUtil {

   private static String dir = "c:/";

   public static void setDiretorio(String d) { dir = d; }
   public static String getDiretorio() { return dir; }
  
   public static void salvar(Object objeto, String arq) throws Exception {
      XMLEncoder xml = null;
      try {
         File f = new File(getDiretorio() + arq);
         xml = new XMLEncoder(
                 new BufferedOutputStream(
                 new FileOutputStream(f)));
         xml.writeObject(objeto);
         xml.flush();
      } finally {
         if (xml != null) {
            xml.close();
         }
      }
   }
  
   public static Object buscar(String arq) throws Exception {
      Object objeto = null;
      XMLDecoder xml = null;
      try {
         File f = new File(getDiretorio() + arq);
         if (!f.exists()) {
            return null;
         }
         xml = new XMLDecoder(
                 new BufferedInputStream(
                 new FileInputStream(f)));
         objeto = xml.readObject();
      } finally {
         if (xml != null) {
            xml.close();
         }
      }
      return objeto;
   }

   public static void main(String args[]) throws Exception {
      String s = "IPA-POO-2011";
      XMLUtil.salvar(s, "Teste.xml");
      System.out.println(XMLUtil.buscar("Teste.xml").toString());
   }
} 

Nenhum comentário:

Postar um comentário