2
I am beginner in programming and am making a small application that should register products, delete, edit, organize, control inventory, cost price, selling price, profit margin, etc.
So far I have managed to get a file . txt to store the product class objects in different lines, thus:
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, so:
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 is closed and I will open it later) I need to store each line of txt in an object Arraylist and then do what is necessary (eg how to sort by price). Any light? Am I on the right track? Is there another way out? Thank you all!
Is it a college job? Does it have to be a text file? Could I use JSON? Why not a database?
– utluiz
It is a work of a course that I did, the teacher left open for us to use creativity and as it is something that he did not teach would have to be as simple as possible, then I thought of storing in txt file (if it is simpler) but it can be done differently... as long as I understand, because I am beginner
– Micael Ferreira