Variavel nao aguenta grande volume de dados

1 resposta
rafaelvannucci

Pessoa, estou com um problema e nao faço ideia de como resolver!
Tenho um programinha simples similar ao bloco de notas, mas estou tendo um grande problema na hora de abrir arquivos grandes, da ordem de 2megas pra cima…
testei varios arquivos e ele lê numa boa, mas na hora de abrir um arquivo que tenha um tamanho mais consideravel, no caso testei um de 2 megas e esperei por mais de 10minutos e o danado do arquivo nao abriu! Oque pode estar ocorrendo???

abaixo meu codigo.:

package buscaemtexto;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import javax.swing.JFileChooser;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.text.StyleConstants;

public class BuscaTextoNew extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;
    JTextPane texto; // componente que permite mudan�a de cores e outros atributos do texto
    JScrollPane painel; // painel com barra de rolagem
    JMenuBar menuBar = new JMenuBar();
    JMenu mArquivo = new JMenu("Arquivo");
    JMenu mAlgoritmo = new JMenu("Algoritmos");
    JMenu mOpções = new JMenu("Opções");
    JMenuItem mLimpar = new JMenuItem("Limpar");
    JMenuItem mLocalizar = new JMenuItem("Localizar");
    JMenuItem mKMP = new JMenuItem("KMP");
    JMenuItem mBoyerMoore = new JMenuItem("BoyerMoore");
    JMenuItem mRabinKarp = new JMenuItem("Rabin Karp");
    JMenuItem mForcaBruta = new JMenuItem("Força Bruta");
    JMenuItem mNovo = new JMenuItem("Novo", KeyEvent.VK_N);
    JMenuItem mAbrir = new JMenuItem("Abrir", KeyEvent.VK_A);
    JMenuItem mSair = new JMenuItem("Sair", KeyEvent.VK_I);
    JMenu mSobre = new JMenu("Sobre");
    JMenuItem mGrupo = new JMenuItem("Grupo");
    JMenuItem mSubstituir = new JMenuItem("Substituir");
    PesquisarGUI pesq = new PesquisarGUI();
    public static String wordPesq;
    public static String subtituir;

    /* adicione as demais op��es do menu aqui... */
    public BuscaTextoNew() {
        super("Trabalho Prático II - Busca em Texto (2010.1.1)");
        mArquivo.setMnemonic(KeyEvent.VK_A);
        menuBar.add(mArquivo);

        mNovo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
        mNovo.addActionListener(this);
        mArquivo.add(mNovo);

        mAbrir.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
        mAbrir.addActionListener(this);
        mArquivo.add(mAbrir);

        mArquivo.addSeparator(); // separador

        mSair.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.ALT_MASK));
        mSair.addActionListener(this);
        mArquivo.add(mSair);

        menuBar.add(mOpções);
        mOpções.add(mLimpar);
        mLimpar.addActionListener(this);

        mOpções.add(mLocalizar);
        mLocalizar.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
        mLocalizar.addActionListener(this);

        mOpções.add(mSubstituir);
        mSubstituir.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
        mSubstituir.addActionListener(this);


        menuBar.add(mAlgoritmo);
        
        menuBar.add(mSobre);
        mSobre.add(mGrupo);
        mGrupo.addActionListener(this);
        
        mBoyerMoore.addActionListener(this);
        mAlgoritmo.add(mBoyerMoore);
        mKMP.addActionListener(this);
        mAlgoritmo.add(mKMP);
        mRabinKarp.addActionListener(this);
        mAlgoritmo.add(mRabinKarp);
        mForcaBruta.addActionListener(this);
        mAlgoritmo.add(mForcaBruta);




        setJMenuBar(menuBar); // adiciona a barra de menus ao frame

        texto = new JTextPane();
        painel = new JScrollPane(texto);

        getContentPane().add(painel, BorderLayout.CENTER);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setVisible(true);

        /*
         * O c�digo abaixo mostra como colorir uma parte do texto
         * com fundo amarelo, come�ando no 5� caractere com comprimento de 4 caracteres:
        StyleConstants.setBackground(texto.getInputAttributes(), Color.YELLOW);
        texto.getStyledDocument().setCharacterAttributes(5, 4, texto.getInputAttributes(), false);
         */
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == mNovo) {
            StyleConstants.setBackground(texto.getInputAttributes(), Color.WHITE);
            texto.setText("");

        }

        if (e.getSource() == mSair) {
            System.exit(0);
        }
        if (e.getSource() == mAbrir) {
            ArrayList <String> aux = new ArrayList<String>();
            String linha = "";
            try {
                JFileChooser arquivo = new JFileChooser();
                int resp = arquivo.showOpenDialog(null);// mostra a janela de diálogo de abrir arquivos
                if (resp != JFileChooser.CANCEL_OPTION) {
                    BufferedReader arq = new BufferedReader(new FileReader(arquivo.getSelectedFile()));

                    while ((linha = arq.readLine()) != null) {
                       aux.add(linha);
                    }
                    texto.setText(aux.toString());
                    arq.close();
                }
            } catch (Exception ex) {
                System.out.println("Erro ao abrir arquivo: " + ex);
            }
        }//fim do abrir

        if (e.getSource() == mLocalizar) {
            pesq.setVisible(true);
        }

        if (e.getSource() == mRabinKarp) {

            byRabinKarp(wordPesq);

        }
        if (e.getSource() == mBoyerMoore) {
            byBoyerMoore(wordPesq);
        }

        if (e.getSource() == mForcaBruta) {
            byForcaBruta(wordPesq);
        }

        if (e.getSource() == mKMP) {
            byKMP(wordPesq);
        }

        if (e.getSource() == mLimpar){
            limpar();
        }

        if (e.getSource() == mSubstituir){
            substituir();
        }

        if (e.getSource() == mGrupo){
            JOptionPane.showMessageDialog(rootPane,("Trabalho Prático de AED2\n"
                                                   +"Professor Virgílio Borges, Faculdade COTEMIG.\n\n"
                                                   +"O objetivo principal do trabalho é demonstrar os quatro algoritmos de busca em texto:\n\n"
                                                   +"BOYER MOORE, RABIN KARP, KMP e FORÇA BRUTA\n\n"
                                                   +"Realizado pelos alunos:\n"
                                                   +"Marcos Vinícius Silvino do Patrocínio\n"
                                                   +"Rafael Vannucci Reis"));
        }

    }

    private void byRabinKarp(String p) {
        int cont = 0, pos = 0, posAtual = 0;
        String txtProcura = this.texto.getText().toString();
        String txtAtual;


        if (pesq.chk2.isSelected()) {
            p = p.toLowerCase();
            do {
                txtAtual = txtProcura.toLowerCase().substring(posAtual, this.texto.getText().toString().length());

                if (cont == 0) {
                    pos = RabinKarp.RKSearch(p, txtAtual);
                    StyleConstants.setBackground(texto.getInputAttributes(), Color.YELLOW);
                    texto.getStyledDocument().setCharacterAttributes(pos, p.length(), texto.getInputAttributes(), false);
                    posAtual += pos + p.length();
                } else {
                    pos = RabinKarp.RKSearch(p, txtAtual);
                    posAtual += pos;
                    StyleConstants.setBackground(texto.getInputAttributes(), Color.YELLOW);
                    texto.getStyledDocument().setCharacterAttributes(posAtual, p.length(), texto.getInputAttributes(), false);
                    posAtual += p.length();
                }

                cont++;
            } while (pos != -1);

        } else {

            do {
                txtAtual = txtProcura.substring(posAtual, this.texto.getText().toString().length());

                if (cont == 0) {
                    pos = RabinKarp.RKSearch(p, txtAtual);
                    StyleConstants.setBackground(texto.getInputAttributes(), Color.YELLOW);
                    texto.getStyledDocument().setCharacterAttributes(pos, p.length(), texto.getInputAttributes(), false);
                    posAtual += pos + p.length();
                } else {
                    pos = RabinKarp.RKSearch(p, txtAtual);
                    posAtual += pos;
                    StyleConstants.setBackground(texto.getInputAttributes(), Color.YELLOW);
                    texto.getStyledDocument().setCharacterAttributes(posAtual, p.length(), texto.getInputAttributes(), false);
                    posAtual += p.length();
                }

                cont++;
            } while (pos != -1);



        }
    }

    private void byBoyerMoore(String p) {
        int cont = 0, pos = 0, posAtual = 0;
        String txtProcura = this.texto.getText().toString();
        String txtAtual;


        if (pesq.chk2.isSelected()) {
            p = p.toLowerCase();
            do {
                txtAtual = txtProcura.toLowerCase().substring(posAtual, this.texto.getText().toString().length());

                if (cont == 0) {
                    pos = BoyerMoore.BMSearch(p, txtAtual);
                    StyleConstants.setBackground(texto.getInputAttributes(), Color.GREEN);
                    texto.getStyledDocument().setCharacterAttributes(pos, p.length(), texto.getInputAttributes(), false);
                    posAtual += pos + p.length();
                } else {
                    pos = BoyerMoore.BMSearch(p, txtAtual);
                    posAtual += pos;
                    StyleConstants.setBackground(texto.getInputAttributes(), Color.GREEN);
                    texto.getStyledDocument().setCharacterAttributes(posAtual, p.length(), texto.getInputAttributes(), false);
                    posAtual += p.length();
                }

                cont++;
            } while (pos != -1);

        } else {

            do {
                txtAtual = txtProcura.substring(posAtual, this.texto.getText().toString().length());

                if (cont == 0) {
                    pos = BoyerMoore.BMSearch(p, txtAtual);
                    StyleConstants.setBackground(texto.getInputAttributes(), Color.GREEN);
                    texto.getStyledDocument().setCharacterAttributes(pos, p.length(), texto.getInputAttributes(), false);
                    posAtual += pos + p.length();
                } else {
                    pos = BoyerMoore.BMSearch(p, txtAtual);
                    posAtual += pos;
                    StyleConstants.setBackground(texto.getInputAttributes(), Color.GREEN);
                    texto.getStyledDocument().setCharacterAttributes(posAtual, p.length(), texto.getInputAttributes(), false);
                    posAtual += p.length();
                }

                cont++;
            } while (pos != -1);



        }
    }

    private void byForcaBruta(String p) {
        int cont = 0, pos = 0, posAtual = 0;
        String txtProcura = this.texto.getText().toString();
        String txtAtual;


        if (pesq.chk2.isSelected()) {
            p = p.toLowerCase();
            do {
                txtAtual = txtProcura.toLowerCase().substring(posAtual, this.texto.getText().toString().length());

                if (cont == 0) {
                    pos = ForcaBruta.forcaBruta(p, txtAtual);
                    StyleConstants.setBackground(texto.getInputAttributes(), Color.RED);
                    texto.getStyledDocument().setCharacterAttributes(pos, p.length(), texto.getInputAttributes(), false);
                    posAtual += pos + p.length();
                } else {
                    pos = ForcaBruta.forcaBruta(p, txtAtual);
                    posAtual += pos;
                    StyleConstants.setBackground(texto.getInputAttributes(), Color.RED);
                    texto.getStyledDocument().setCharacterAttributes(posAtual, p.length(), texto.getInputAttributes(), false);
                    posAtual += p.length();
                }

                cont++;
            } while (pos != -1);

        } else {

            do {
                txtAtual = txtProcura.substring(posAtual, this.texto.getText().toString().length());

                if (cont == 0) {
                    pos = ForcaBruta.forcaBruta(p, txtAtual);
                    StyleConstants.setBackground(texto.getInputAttributes(), Color.RED);
                    texto.getStyledDocument().setCharacterAttributes(pos, p.length(), texto.getInputAttributes(), false);
                    posAtual += pos + p.length();
                } else {
                    pos = ForcaBruta.forcaBruta(p, txtAtual);
                    posAtual += pos;
                    StyleConstants.setBackground(texto.getInputAttributes(), Color.RED);
                    texto.getStyledDocument().setCharacterAttributes(posAtual, p.length(), texto.getInputAttributes(), false);
                    posAtual += p.length();
                }

                cont++;
            } while (pos != -1);
        }
    }

    private void byKMP(String p) {
        int cont = 0, pos = 0, posAtual = 0;
        String txtProcura = this.texto.getText().toString();
        String txtAtual;


        if (pesq.chk2.isSelected()) {
            p = p.toLowerCase();
            do {
                txtAtual = txtProcura.toLowerCase().substring(posAtual, this.texto.getText().toString().length());

                if (cont == 0) {
                    pos = KMP.KMPSearch(p, txtAtual);
                    StyleConstants.setBackground(texto.getInputAttributes(), Color.LIGHT_GRAY);
                    texto.getStyledDocument().setCharacterAttributes(pos, p.length(), texto.getInputAttributes(), false);
                    posAtual += pos + p.length();
                } else {
                    pos = KMP.KMPSearch(p, txtAtual);
                    posAtual += pos;
                    StyleConstants.setBackground(texto.getInputAttributes(), Color.LIGHT_GRAY);
                    texto.getStyledDocument().setCharacterAttributes(posAtual, p.length(), texto.getInputAttributes(), false);
                    posAtual += p.length();
                }

                cont++;
            } while (pos != -1);

        } else {

            do {
                txtAtual = txtProcura.substring(posAtual, this.texto.getText().toString().length());

                if (cont == 0) {
                    pos = KMP.KMPSearch(p, txtAtual);
                    StyleConstants.setBackground(texto.getInputAttributes(), Color.LIGHT_GRAY);
                    texto.getStyledDocument().setCharacterAttributes(pos, p.length(), texto.getInputAttributes(), false);
                    posAtual += pos + p.length();
                } else {
                    pos = KMP.KMPSearch(p, txtAtual);
                    posAtual += pos;
                    StyleConstants.setBackground(texto.getInputAttributes(), Color.LIGHT_GRAY);
                    texto.getStyledDocument().setCharacterAttributes(posAtual, p.length(), texto.getInputAttributes(), false);
                    posAtual += p.length();
                }

                cont++;
            } while (pos != -1);
        }
    }

    private void limpar (){
        StyleConstants.setBackground(texto.getInputAttributes(), Color.WHITE);
        texto.getStyledDocument().setCharacterAttributes(0, texto.getText().toString().length(), texto.getInputAttributes(), false);

    }

    public void substituir(){
        String aux = texto.getText().toString().replaceAll(wordPesq,subtituir);
        texto.setText(aux);

    }

    public static void main(String[] args) {
        new BuscaTextoNew();
    }
}

1 Resposta

B

Tente usar StringBuilder ao invés de adicionar as strings em um ArrayList. Adicionando no ArrayList você tem várias referências para várias Strings no Stack.

StringBuilder sb = new StringBuilder();

for ...
sb.append(linha);

...
Criado 26 de julho de 2010
Ultima resposta 26 de jul. de 2010
Respostas 1
Participantes 2