The implementation below is very faithful to the code you made, but now it’s working, I commented the problems I saw:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class Arquivo {
private ArrayList<String> arq_frases = new ArrayList<>(); //poderia ser lowerCamelCase: "arqFrases"
public void leitor() throws IOException { //seria melhor um nome como "ler" já que é o comportamento do objeto, "Leitor" parece o nome de um objeto
BufferedReader buffRead = new BufferedReader(new FileReader("arquivo.txt")); //"arquivo.txt" poderia ser passado por parâmetro para o método ser mais reutilizável
String linha = "";
while (true) {
if (linha != null) {
arq_frases.add(linha); //aqui adiciona-se primeiro a linha vazia, depois as linhas do arquivo
//dessa forma, o ArrayList fica com uma String vazia no índice 0
} else {
break;
}
linha = buffRead.readLine();
}
buffRead.close();
}
public static void main(String[] args) {
Arquivo arquivo = new Arquivo(); //Como coloquei o ArrayList como variável de instância, precisaremos instanciar a classe Arquivo
try {
arquivo.leitor(); //carrega arq_frases com as linhas do arquivo
} catch (IOException e) {
System.out.println("Ops! problema ao ler o arquivo!");
e.printStackTrace();
}
for (String linha : arquivo.arq_frases) { //acessando o ArrayList da instância
System.out.println(linha);
}
}
}
Below follows an alternative implementation using the class Files:
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.stream.Collectors;
public class Arquivo {
public static List<String> lerLinhasDeArquivo(String caminhoDoArq) throws IOException {
return Files.lines(new File(caminhoDoArq).toPath(), StandardCharsets.ISO_8859_1).collect(Collectors.toList());
}
public static void main(String[] args) throws IOException {
lerLinhasDeArquivo("arquivo.txt").forEach(System.out::println);
}
}
In this example, lerLinhasDeArquivo(...) is static, and therefore it is not necessary to instantiate Arquivo to access it.
Try the
staticalong with thepublic.– SeventhBit
"mesmo usando Extends Arquivo ou Instânciando o Objeto Arquivo em outra classe", by this phrase I have the impression that you have not yet understood the basic mechanisms of Java, which are much more fundamental and important than reading files, I recommend studying them first, with books or lessons so that your knowledge does not remain with important gaps on the fundamentals.– Douglas