Pegar texto de arquivo txt

1 resposta
programaçãojava
azulao98

Estou tendo dificuldades para pegar o texto de um arquivo txt, segue o código:

private ArrayList<String> carregaTxt(){
    ArrayList<String> resultado = new ArrayList<String>();
   
    Scanner ler = new Scanner(System.in);

    System.out.printf("\nConteúdo do arquivo texto:\n");
     try {
    
    JFileChooser nomeArquivo = new JFileChooser();
    nomeArquivo.setCurrentDirectory(new File(System.getProperty("user.home")));
    int result = nomeArquivo.showOpenDialog(this);
    if (result == JFileChooser.APPROVE_OPTION) {
    File selectedFile = nomeArquivo.getSelectedFile();
    System.out.println("Selected file: " + selectedFile.getAbsolutePath());

}

String nome = nomeArquivo.getText();
  FileReader arq = new FileReader(nome);
  BufferedReader lerArq = new BufferedReader(arq);

  String linha = lerArq.readLine();

  while (linha != null) {
    System.out.printf("%s\n", linha);
      
    String[] palavrasDaLinha = linha.split(" ");
    
    for(String palavra : palavrasDaLinha) {
          
        if( palavra.trim().length() > 1 && !"".equals(palavra.trim())) {
            resultado.add(palavra);  
        }

    }

    linha = lerArq.readLine(); 
  }

  arq.close();

  System.out.println("\n");
  System.out.printf("Total de palavras no arquivo: %s\n", resultado.size());

} catch (IOException e) {
    System.err.printf("Erro na abertura do arquivo: %s.\n",
      e.getMessage());
}   
          
    return resultado;  
}

1 Resposta

Jederson_Andre

Não entendi essa paradinha !"".equals( // ){}

Criado 4 de outubro de 2017
Ultima resposta 4 de out. de 2017
Respostas 1
Participantes 2