can't get output to file to work - please help

Hi all,

I'm having trouble printing to my text file. What I've done is created a main method that sets up the file output and print streams. I then create a JFrame, myGui. The GUI has action listeners that make it so when I push a button, code executes p.print("whatever") to the file, where p is a prinstream that prints to my .txt file. These, however, do not print to file - I assume because they're methods that execute after main finishes. Any p.print statements work properly if I put them in main, but none of the p.print statements work in the action listener methods. Can someone please help me? I'd like the print statements in the action listener to work. Thanks in advance.

jboolean23

publicstaticvoid main(String[] args){

FileOutputStream out;// declare a file output object

PrintStream p;// declare a print stream object

try

{

// Create a new file output stream

// connected to "myfile.txt"

out =new FileOutputStream("myfile.txt");

// Connect print stream to the output stream

p =new PrintStream( out );

}

catch (Exception e)

{

System.err.println ("Error writing to file");

}

GUI myGUI =new GUI();

}

my actionlistener looks something like:

publicvoid actionPerformed(ActionEvent e){

if (e.getSource() == aButton){

p.println("Whatever I try to print here does not actually get printed");

}

}

[2329 byte] By [jboolean23a] at [2007-10-3 10:45:36]
# 1
close your file after writing to it.p.close();
Navy_Codera at 2007-7-15 6:09:57 > top of Java-index,Java Essentials,Java Programming...
# 2
if your PrintStream p is defined in your main function, then how does the ActionListener have access to it?
paul.y.wanga at 2007-7-15 6:09:57 > top of Java-index,Java Essentials,Java Programming...
# 3

Try this out:

import javax.swing.*;

import java.io.*;

public class Test extends JFrame {

private JTextArea jta;

private JScrollPane jsp;

private JMenuBar jmb;

private JMenu fileMenu;

private JMenuItem saveDoc;

private JMenuItem exitItem;

public Test() {

jta = new JTextArea();

jsp = new JScrollPane(jta);

jmb = new JMenuBar();

fileMenu = new JMenu("File");

saveDoc = new JMenuItem("Save ...");

exitItem = new JMenuItem("Exit");

fileMenu.add(saveDoc);

fileMenu.addSeparator();

fileMenu.add(exitItem);

jmb.add(fileMenu);

this.getContentPane().add(jsp);

this.pack();

this.setSize(400, 400);

this.setJMenuBar(jmb);

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

saveDoc.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

saveFile();

}

});

exitItem.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

System.exit(0);

}

});

}

private void saveFile() {

JFileChooser jfc = new JFileChooser();

int option = jfc.showSaveDialog(null);

if (option == JFileChooser.APPROVE_OPTION) {

try {

FileOutputStream fos = new FileOutputStream(jfc.getSelectedFile());

PrintWriter pw = new PrintWriter(fos);

pw.println(jta.getText());

pw.close();

} catch (IOException e) { e.printStackTrace(); }

}

}

public static void main(String[] argv) { new Test().setVisible(true); }

}

Navy_Codera at 2007-7-15 6:09:57 > top of Java-index,Java Essentials,Java Programming...
# 4
Navy_Coder: I already did that at the end of main, the code I posted is just a little simplified. Closing the printstream doesn't fix the problempaul.y.wang: It's a static printstream
jboolean23a at 2007-7-15 6:09:57 > top of Java-index,Java Essentials,Java Programming...
# 5
Thanks for your quick replies guys.NavyCoder: Actually I don't have a text area. I have been printing to the console. Everytime I print to the console, I also want to print to the text file.
jboolean23a at 2007-7-15 6:09:57 > top of Java-index,Java Essentials,Java Programming...
# 6

> Navy_Coder: I already did that at the end of main,

> the code I posted is just a little simplified.

> Closing the printstream doesn't fix the problem

>

>

> aul.y.wang: It's a static printstream

which goes back to my question. printstream is a local static defined in your main method where you initialized the stream. how does your gui object have access to a local static variable inside main? it sounds more like you failed to properly initialize your stream before you tried to write to it.

paul.y.wanga at 2007-7-15 6:09:57 > top of Java-index,Java Essentials,Java Programming...
# 7

> which goes back to my question. printstream is a

> local static defined in your main method where you

> initialized the stream. how does your gui object have

> access to a local static variable inside main? it

> sounds more like you failed to properly initialize

> your stream before you tried to write to it.

If that were the case then he'd be getting a NullPointerException. He didn't mention that.

@OP: Doesn't matter if you're using a JTextArea or not. My example shows how to write a String to a PrintWriter (getText() returns a String). But, here is another simple example that may be able to help straighten out any questions you have:

import java.io.*;

import javax.swing.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

public class Test extends JFrame {

private FileOutputStream fos;

private PrintWriter pw;

private JButton button;

public Test() {

try {

fos = new FileOutputStream("test.txt");

pw = new PrintWriter(fos);

} catch (IOException e) { e.printStackTrace(); }

button = new JButton("Write to File And Exit");

getContentPane().setLayout(new java.awt.FlowLayout());

getContentPane().add(button);

pack();

setTitle("Example");

setLocationRelativeTo(null);

setDefaultCloseOperation(EXIT_ON_CLOSE);

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

writeToFile();

}

});

}

private void writeToFile() {

pw.println("This is being printed to the file.");

System.out.println("Wrote to the file.");

pw.close();

System.exit(0);

}

public static void main(String[] argv) { new Test().setVisible(true); }

}

Navy_Codera at 2007-7-15 6:09:57 > top of Java-index,Java Essentials,Java Programming...