Is there any way to display a txt file without Jfilechooser?

Asked

Viewed 101 times

0

Is there any way to display a txt file without Jfilechooser ? For example by typing the location of . txt file and connecting directly to Jtextarea

  • You can explain better what you want to do?

  • 1

    You can create a reference to a file from any String by doing new File(caminho), where caminho can be the content of any text field. That would be?

1 answer

1


follows below two good references and an example code.

Devmedia - Reading Txt Data with Java: http://www.devmedia.com.br/lendo-dados-de-txt-com-java/23221

Caelum - Reading Java Text Files with Scanner: http://blog.caelum.com.br/lendo-arquivos-texto-em-java/

public static void main(String[] args) {

    Scanner ler = new Scanner(System.in);
    System.out.printf("Informe o nome de arquivo texto:\n");
    String caminhoArquivo = ler.nextLine();
    System.out.printf("\nConteúdo do arquivo texto:\n");

    try {
        try (FileReader arquivo = new FileReader(caminhoArquivo)) {
            BufferedReader lerArq = new BufferedReader(arquivo);

            // lê a primeira linha
            // a variável "linha" recebe o valor "null" quando o processo
            // de repetição atingir o final do arquivo texto
            String linha = lerArq.readLine();

            while (linha != null) {
                System.out.printf("%s\n", linha);     
                linha = lerArq.readLine(); // lê da segunda até a última linha
            }
        }
    } catch (IOException e) {
        System.err.printf("Erro na abertura do arquivo: %s.\n",e.getMessage());
    }
}
  • thanks was just the way I was thinking about doing !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.