Olá a todos,
Estou começando a estudar algumas funcionalidades da java.io
Estava lendo esse artigo no próprio forum: http://www.guj.com.br/articles/13
Tudo certo quando a isso
Mas por que não posso mandar um println pegando o texto que há no meu campo?
Ele cria o arquivo mas não salva nada >.<
package estudo;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;
import javax.swing.*;
public class Estudo extends JFrame
{
private static final long serialVersionUID=1L;
private JPanel painel;
private JLabel label;
private JTextField campo;
FileWriter arquivo;
PrintWriter saida;
public Estudo()
{
super("!");
painel = new JPanel();
getContentPane().add(painel);
label = new JLabel("Criar Texto");
campo = new JTextField(10);
campo.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent ex)
{
if(ex.getKeyCode() == KeyEvent.VK_ENTER)
{
while(campo.getText() != "")
{
//Não salva nenhuma linha
saida.println("Lendo: "+campo.getText());
saida.flush();
campo.setText("");
}
}
}
});
painel.add(label);
painel.add(campo);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setBounds(500,100,200,150);
setVisible(true);
ler();
}
public void ler()
{
try
{
arquivo = new FileWriter("C:/Users/Allan/Desktop/Arquivo.txt");
saida = new PrintWriter(arquivo);
saida.println("ESSA LINHA ELE SALVA");
}catch(Exception e){System.err.println("Erro");}finally{
try {
saida.close();
arquivo.close();
} catch (IOException e) {
e.printStackTrace();
}}
}
}
Agradeço!