Problemas com entrada/saida

2 respostas
hcbelias

Eu tenho um programa que a partir de uma determinada pasta com arquivos html, ele pega cada um dos arquivos html, transforma em codigo html, faz algumas formatacoes especificas, e grava em um arquivo de saida.
obs::: A formatacao de todos codigos devem estar em um arquivo de saida apenas…
Nao estou conseguindo fazer a gravacao da formatacao em um arquivo de saida


package nequim;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

public class Main {

public Main() {
}

public static File emCodigoHtml(File arquivo) throws IOException {

File arquivohtml = new File("/home/henrique/programa/",arquivo.getName());

FileReader leitor = new FileReader(arquivohtml);

char[] conteudo = new char[ (int) arquivohtml.length() ];

leitor.read( conteudo );

leitor.close();

String texto = new String( conteudo );
File arquivotxt=new File("/home/henrique/programa/",“entradatxt.txt);

FileWriter gravador=new FileWriter(arquivotxt);

PrintWriter imprime=new PrintWriter(gravador);

imprime.print(texto);

imprime.close();

return arquivotxt;

}//Testado!!

public static Boolean paraEdicao(String linha) {

Boolean quit=false;

String str=linha;

str=str.trim();

for (int i=0;i<str.length()-5;i++) {

if (str.regionMatches(true,i,"",0,6)) {

quit=true;

break;

}

}
return quit;

}//Testado!!

public static String editaExplore(String linha) {

String str=linha;

for (int i=0;i<str.length()-6;i++) {

if (str.regionMatches(true,i,Explore,0,7)) {//Verifica se na linha h� explore

StringBuffer strbuffer=new StringBuffer(str);

str=strbuffer.insert(i+7,"\n").toString();

}

}

return str;

}//Testado!!
public static String editaMore(String linha) {

String str=linha;

for (int i=0;i<str.length()-3;i++) {

if (str.regionMatches(true,i,more,0,4)) {//Verifica se na linha h� explore

StringBuffer strbuffer=new StringBuffer(str);

str=strbuffer.insert(i+4,"\n").toString();

}

}

return str;
}//Testado!!

public static String editaTag(String linha,BufferedReader br) throws IOException {

String temp="";

String saida="";

String str=linha;

String exp1=HREF;

String exp2=href;

int cont, abre=str.indexOf("<"), fecha=str.indexOf(">"),

ultimoabre=str.lastIndexOf("<"), ultimofecha=str.lastIndexOf(">");

cont=fecha;

if (ultimoabre>ultimofecha)//caso o abrechave apareca na linha de baixo
{

//Aqui devo concatenar a string da linha de baixo, com a linha atual
str=str.concat(br.readLine());

}

if (abre<0 | fecha<0)//caso nao ha abre e fecha chaves

{

saida=str;

} else {

for (int i=0;i<ultimoabre+1 {

if (str.startsWith("<",i)) {

//comeca a tag, deve-se verificar a tag ,e formata-la ou nao

String aux=("");

if (fecha==(str.length()-1)) {

aux=str.substring(i);

} else {

aux=str.substring(i,str.indexOf(">",i)+1);

}//Separa a subtring a ser analisada

String verifica=aux;
if ((verifica.matches(".<em>"+exp1+".</em>")) | (verifica.matches(".<em>"+exp2+".</em>"))) {

//nao faz nada

} else {

aux=("|");

}

saida=saida.concat(aux.toString());

abre=str.indexOf("<",i);

fecha=str.indexOf(">",i);

i=str.indexOf(">",i)+1;
} else {

//nao ha tag

saida=saida.concat(str.substring(i,i+1));
abre=str.indexOf("<",i);

fecha=str.indexOf(">",i);

i++;

}

}

}

return saida;
}//Testado!!

public static String editaBarra(String linha) {

String str=linha;
str=str.replaceAll("\|(\s*\|)*", “|”);

return str;
}//Testado!!

public static void main(String[] args) throws IOException {

File pasta=new File("/home/henrique/programa/");

File saida=new File("/home/henrique/programa/",saidatxt.txt);

FileWriter fw=new FileWriter(saida.txt,true);

PrintWriter pw=new PrintWriter(fw);

String []listaarquivos=pasta.list();

for (int i=0;i<listaarquivos.length;i++)

{
File entradahtml=new File(listaarquivos[i]);

File entradatxt=emCodigoHtml(entradahtml);

FileReader fr=new FileReader(entradatxt);

BufferedReader br=new BufferedReader(fr);

String linha=br.readLine();

while (!paraEdicao(linha)) {

if (linha.length()<1) {

linha=br.readLine();

continue;

}

linha=br.readLine();

}

linha=br.readLine();//le a proxima linha apos o 

linha=br.readLine();//le a proxima linha, que possui as informacoes de lig identicos

fr.close();

linha=editaExplore(linha);

linha=editaMore(linha);

linha=editaTag(linha,br);

linha=editaBarra(linha);

pw.println(linha);
}

pw.close();

fw.close();

}

}

O que esta errado??

2 Respostas

Zakim

onde que da o erro?

coloque o seu codigo entre a tag [code] para ficar mais facil de entender e tbm documente as linhas que vc tem duvida!

desse jeito é meio complicado analisar...

valeu

K

AJUDA… URGENTE…
estou fazendo um sistema para fazer impressão de etiqueta…
estou com um problema… o erro acusa q o erro esta… no
PrintService impressora = PrintServiceLookup.lookupDefaultPrintService();

eu compilo o arquivo para executar a classe…
me recorna q tem um suporte…
identifica a impressora q esta instalada…

só q n impimi…
aparece esses erros

Exception in thread main java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

at java.util.ArrayList.checkBoundExclusive(libgcj.so.70)

at java.util.ArrayList.get(libgcj.so.70)

at gnu.javax.print.CupsServer.getDefaultPrinter(libgcj.so.70)

at gnu.javax.print.CupsPrintServiceLookup.getDefaultPrintService(libgcj.so.70)

at javax.print.PrintServiceLookup.lookupDefaultPrintService(libgcj.so.70)

at Print.(Print.java:32)

at Print.main(Print.java:93)
Criado 25 de julho de 2007
Ultima resposta 27 de jul. de 2007
Respostas 2
Participantes 3