- http://www.dcc.ufrj.br/~comp2/TextosJava/Excecoes%20em%20Java.pdf
- http://www.dcc.ufam.edu.br/horacio/images/stories/Disciplinas/PP/PP-Aula7-Excecao.pdf
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());
}
}
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());
}
}