Problemas com PrintWriter

3 respostas
J

No meu editor grafico to querendo criar uma opção de salvar as figuras que criei. A minha ideia é passar todos os objetos criados para um List de Strings e salvar estas Strings em um arquivo txt. Só que sei lá porque não esta sendo salvo os dados no txt. Já fiz varios teste e nada! Vou postar as principais partes do código:

Onde crio a janela
class Main
{
	public static void main(String[] args)
	{
		JanelaPrincipal w = new JanelaPrincipal(300, 200, "Nossa Primeita GUI");
	}
}

Aonde crio o botão salvar e add nele o tratador de eventos:

import java.util.*;
import java.awt.*;
import java.io.IOException;
import java.awt.event.*;

import javax.swing.*;

class JanelaPrincipal extends JFrame
{
	private JMenuBar	barra;
	private JMenuItem   salvar; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

       JanelaPrincipal(int sx, int sy, String title)
	{
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(sx, sy);
		this.setTitle(title);
		
		this.getContentPane().add(q);
		
		barra = new JMenuBar();
		this.setJMenuBar(barra);
	
		salvar = new JMenuItem("Salvar"); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		m_arq.add(salvar);
		
		Click aClick;
		salvar.addActionListener(aClick); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		
		this.setVisible(true);
	}

       private class Click implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			Salvar s = new Salvar();
			
			if(e.getSource() == mi_comPreench)
				preench = true;
			else if(e.getSource() == mi_semPreench)
				preench = false;
			else if(e.getSource() == mi_undo)
				q.undo();
			else if(e.getSource() == mi_redo)
				q.redo();
			else if(e.getSource() == mi_sair)
				System.exit(0);
			else if(e.getSource() == salvar){  // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
			    s.salve();	
					
			}		
		}
	}

Classe responsavel por salvar os dados no arquivo:

import java.util.*;
import java.io.*;
import java.io.FileNotFoundException;

class Salvar {
   
    public Salvar(){
    }
   
    Quadro q = new Quadro();
    private java.util.List<String> objetos_salvos = new ArrayList<String>(); // Para armazenar os objetos em sua forma string
   
    public void salve(){
    	
     //reps é um array da classe Quadro que guarda nele todos os objetos que são criados e que logo após a sua criação devem ser printados na tela!

    	for(int i = 0; i<q.reps.size(); i++){
    	    objetos_salvos.add(q.reps.get(i).toString());
    	}
    	   	try{
			  
				salveNoArq();
		    }
				
				catch(IOException b){
				}	
    }	

    public void salveNoArq() throws IOException{

    int i = 0;
    PrintWriter outArq = new PrintWriter(new BufferedWriter(new FileWriter("objsalvos.txt")));
    
     while(objetos_salvos.size()>i){
    	
   
       outArq.println(objetos_salvos.get(i));
       outArq.close();
       i++;
    
    }
    
   }
}

O arquivo txt fica em branco! Já fiz uns testes colocando uns System.out.println("bla") em vários pontos do meu código e é como se o while do metodo salveNoArq() não fosse executado! Mas não faz sentido! Já que o tamanho do List é maior que a variavel i!!!!

Não sei mais o que fazer!
Se alguem poder ajudar agradeço!

3 Respostas

R
public void salveNoArq() throws IOException {
  int i = 0;
  PrintWriter outArq = new PrintWriter(new BufferedWriter(new FileWriter("objsalvos.txt")));

  while (i < objetos_salvos.size()) {
    outArq.println(objetos_salvos.get(i));
    i++;
  }

  outArq.close();  
}
J
roger_rf:
public void salveNoArq() throws IOException {
  int i = 0;
  PrintWriter outArq = new PrintWriter(new BufferedWriter(new FileWriter("objsalvos.txt")));

  while (i < objetos_salvos.size()) {
    outArq.println(objetos_salvos.get(i));
    i++;
  }

  outArq.close();  
}

Infelizmente o problema ainda persiste! O txt permanece em branco e o tamanho dele fica em 0 KB!!!!!!

R
public void salveNoArq() throws IOException {
  int i = 0;
  BufferedWriter outArq = new BufferedWriter(new FileWriter("objsalvos.txt"));

  while (i < objetos_salvos.size()) {
    outArq.write(objetos_salvos.get(i).toString());
    outArq.newLine();
    i++;
  }

  outArq.close();
}
Criado 15 de maio de 2009
Ultima resposta 15 de mai. de 2009
Respostas 3
Participantes 2