/* * Documento.java * * Created on 29 de Março de 2006, 21:58 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package business; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; /** * * @author lchernicharo */ public class Documento { private String nomeArquivo; private String conteudo; private String caminho; /** Creates a new instance of Documento */ public Documento() { conteudo = ""; caminho = ""; nomeArquivo = ""; } public Documento( String fName ){ conteudo = ""; splitPath( fName ); } public String getNomeArquivo() { return nomeArquivo; } public void setNomeArquivo(String nomeArquivo) { this.nomeArquivo = nomeArquivo; } public String getConteudo() { return conteudo; } public void setConteudo(String conteudo) { this.conteudo = conteudo; } public void load() throws IOException, FileNotFoundException{ BufferedReader arq = null; try { arq = new BufferedReader( new FileReader( caminho + nomeArquivo ) ); StringBuffer sb = new StringBuffer(); String linha = arq.readLine(); while( linha != null ){ sb.append( linha + "\r\n" ); //faço isso pq o readLine tira o fim da linha linha = arq.readLine(); } conteudo = sb.toString().trim(); } catch (FileNotFoundException ex) { throw new FileNotFoundException( "Caminho ou nome de arquivo inválido." ); } catch (IOException ex) { throw new IOException( "Erro de I/O ao abrir o arquivo." ); } finally{ arq.close(); } } public void load( String fName ) throws IOException, FileNotFoundException{ splitPath(fName); load(); } private void splitPath(final String fName) { int pos = fName.lastIndexOf( "\\" ) + 1; caminho = fName.substring( 0, pos ); nomeArquivo = fName.substring( pos, fName.length() ); } public void save() throws IOException{ BufferedWriter arq = null; try { arq = new BufferedWriter( new FileWriter( caminho + nomeArquivo ) ); arq.write( conteudo ); } catch (IOException ex) { throw new IOException( "Erro de I/O ao salvar o arqivo." ); } finally{ arq.flush(); arq.close(); } } public String getCaminho() { return caminho; } public void setCaminho(String caminho) { this.caminho = caminho; } }