Entrada e Saida

1 resposta
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,"</div>",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(".*"+exp1+".*")) | (verifica.matches(".*"+exp2+".*"))) {
                    //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 </div>
        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??

1 Resposta

fsquadro

Por favor,

Não duplique os tópicos.

http://www.guj.com.br/posts/list/65384.java

Obrigado.

Criado 25 de julho de 2007
Ultima resposta 25 de jul. de 2007
Respostas 1
Participantes 2