I'm a beginner in programming and I'm making a small application that should register products, delete, edit, organize, track inventory, cost price, sales price, profit margin, etc.
So far I've been able to get a .txt file to store the objects of the product class on different lines, like this:
public void gravaProduto() throws IOException{
FileWriter arquivo = new FileWriter("E:\Documentos\NetBeansProjects\Experimentacao3\cadastro.txt",true);
PrintWriter gravarArquivo = new PrintWriter(arquivo);
for (Produto p : produtos) {
gravarArquivo.println(p);
}
arquivo.flush(); //libera a gravaçao
arquivo.close(); //fecha o arquivo
}
I was also able to read the file, like this:
public void leProduto() throws IOException{
String linha = "a";
FileReader arq = new FileReader("E:\Documentos\NetBeansProjects\Experimentacao3\cadastro.txt");
//armazenando conteudo no arquivo no buffer
BufferedReader lerArq = new BufferedReader(arq);
//lendo a primeira linha
//String linha = lerArq.readLine();
//a variavel linha recebe o valor 'null' quando chegar no final do arquivo
while (linha != null){
System.out.printf("%s\n",linha);
//lendo a segundo até a última
linha = lerArq.readLine();
}
arq.close();
}
My problem is: (after the application terminates and I will open it later) I need to store each line of the txt in an ArrayList of objects and then do whatever it takes sort by price). Any light? I'm in the right way? Do you have another outlet? Thank you all!