How to display a .txt file in a JtextArea automatically

4

I'm tired of searching and can not find how to display a .txt file in a JTextArea automatically. I made a program that opens a window and then wanted it to display the contents of that file without having to load it into a button.

    
asked by anonymous 07.11.2014 / 23:10

2 answers

5

You should create the JTextArea within the constructor!

Here's an example:

  

DemoJTextArea.java

public class DemoJTextArea {
    public static void main(String[] args) {
        Janela jn = new Janela();
        jn.setVisible(true);
    }
}
  

Window.java

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JTextArea;

class Janela extends JFrame {
    public Janela() {
        super("Display txt on JTextArea!");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(250, 250);

        File file = new File("/home/anderson/arquivo.txt");
        FileInputStream fis = null;
        String texto = "";

        try {
            fis = new FileInputStream(file);
            int content;
            while ((content = fis.read()) != -1) {
                texto += (char) content;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        JTextArea textArea = new JTextArea(texto);
        textArea.setLineWrap(true); //quebra de linha automática
        add(textArea);
    }
}

Running:

Reference: How To Read File In Java - FileInputStream

    
08.11.2014 / 01:41
0

I have been able to do it in a different way but it gives the same result.

I made the visual screen constructor call the Reader (); procedure by passing the file address, the reader reads what is in the file and writes everything to the ArrayList contentTxt that is public static .

Returning to the constructor after calling the reader will move the entire ArrayList content to a String named line with the help of a for , then we give an AreaText. setText (line); and ready we have a program that loads the information from the .txt file into an AreaText.

Down codes are missing some things like imports and instantiating for the program to run correctly. I just put what's in evidence for the solution.

public class JanelaArquivosTxt extends javax.swing.JFrame {

    ArquivosTxt tela1 = new ArquivosTxt();

    public JanelaArquivosTxt() {
      initComponents();


      leitor("C:\Users\Paulo Brito\Desktop\paulo.txt");

      String linha = "";
      for (int i = 0; i < conteudoTxt.size(); i++) {

        linha += conteudoTxt.get(i) + "\n";            
      }
      AreaTexto.setText(linha);

    }
    private javax.swing.JTextArea AreaTexto;
}

public class ArquivosTxt {

  public static ArrayList < String > conteudoTxt = new ArrayList < String > ();
  private static File arquivo;

  public static void leitor(String path) {

    try {
      BufferedReader buffRead = new BufferedReader(new FileReader(path));
      String linha = "";

      if (!arquivo.exists()) {

        arquivo.createNewFile();
      }

      ArrayList < String > linhas = new ArrayList < String > ();

      while (true) {
        linha = buffRead.readLine();
        if (linha != null) {
          linhas.add(linha);

        } else {
          break;
        }
      }

      buffRead.close();

      conteudoTxt = linhas;

    } catch (IOException ex) {
      Logger.getLogger(JanelaArquivosTxt.class.getName()).log(Level.SEVERE, null, ex);
    }

  }

}
    
02.12.2014 / 17:40