Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing to file problems  RSS feed

Derek Duggan
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks for any help i got with my last problem, an other small problem i have. I'm tring to use a gui to write information to a file, its compling with no errors, creates the file but won't write any thing to it. I have it inside the try and catch no luck i've tried it out side the try and catch also. The same problem

Any help would be great i know its only something small just can't see it



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Create_Mail extends JPanel implements ActionListener
private JButton b1;
private JButton b4;
private JButton b2;
private JButton b3;
private JLabel l4;
private JLabel l5;
private JTextArea ta6;
private JComboBox comb7;
private JTextField t8;
private JTextField t9;
private JButton b10;
private FileOutputStream fos;
private PrintWriter out;

public Create_Mail() {
//construct preComponents
String[] jcomp7Items = {"Ireland", "IP 1", "IP 2", "IP 3", "IP 4", "IP 5", "America", "IP1", "IP2", "IP3"};
//construct components
b1 = new JButton ("Save E-Mail");
b4 = new JButton ("a");
b2 = new JButton ("Exit System");
b3 = new JButton ("Main Menu");
l4 = new JLabel (" To :");
l5 = new JLabel (" Subject :");
ta6 = new JTextArea (5, 5);
comb7 = new JComboBox (jcomp7Items);
t8 = new JTextField (5);
t9 = new JTextField (5);
b10 = new JButton ("Calculate Size of E-Mail");
//adjust size and set layout
setPreferredSize (new Dimension (482, 488));
setLayout (null);
//add components
add (b4);
add (b1);
add (b2);
add (b3);
add (l4);
add (l5);
add (ta6);
add (comb7);
add (t8);
add (t9);
add (b10);
//set component bounds
b1.setBounds (35, 360, 100, 20);
b2.setBounds (160, 395, 105, 20);
b3.setBounds (160, 360, 100, 20);
b4.setBounds (100, 460, 95, 20);
b10.setBounds (285, 360, 180, 20);
l4.setBounds (25, 30, 100, 25);
l5.setBounds (25, 80, 100, 25);
ta6.setBounds (30, 150, 435, 165);
comb7.setBounds (365, 35, 95, 20);
t8.setBounds (135, 85, 210, 20);
t9.setBounds (135, 35, 210, 20);

public void actionPerformed( ActionEvent e )
//When the user hits the below button a comparsion is made
//If it's true then its executes
//The process is continued depending on the button the user hits

System.out.println("button => "+ e.getActionCommand());

if(( new String("a")).compareTo(e.getActionCommand()) == 0 )

if(( new String("Save E-Mail")).compareTo(e.getActionCommand()) == 0 )


fos = new FileOutputStream("E-mail.txt", true );
out = new PrintWriter(new OutputStreamWriter(fos));

String s1 = ta6.getText();
String s2 = t8.getText();
String s3 = t9.getText();
System.out.println("Test in dos prompt");
out.println(s1 +"," +s2 +"," +s3);
catch (Exception ex){}


public static void main (String[] args)
JFrame frame = new JFrame ("Create_Mail");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new Create_Mail());
frame.setVisible (true);
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to close() streams when you're done with them to clean up resources and also to flush output streams. I suspect that when the JVM exits it will close the streams for you (don't count on that), but perhaps in this case you're checking the file while the program is still running? Note that when you close a stream that wraps another stream, it closes the inner stream -- all the way down to the FileOutputStream in this case since they're chained.

You can do this immediately before the end of the try block.Here are a few other notes. In the future, please surround your source code with the UBB Code tags: [ code ] ... [ /code ] (no spaces). You can use the buttons below the "Add Reply" button instead of typing too.Two things. First, a String literal like "a" or "Save E-mail" is a String. You don't need to use new String("a"). Second, Object.compareTo(Object) depends on the object implementing Comparable, which most classes do not. To see if any two objects are equal, use Object.equals(Object) that returns true or false. The above simplifies toStrings are immutable (they cannot be modified). All of the methods that seem to modify the String -- trim(), substring(), toUpperCase(), etc. -- actually return a new String with the function applied. Either change the trim() calls toor combine them with the getText() calls.
[ January 15, 2005: Message edited by: David Harkness ]
Gregg Bolinger
Ranch Hand
Posts: 15304
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even though you are doing this in a GUI, it really has nothing to do with GUI stuff, so I am moving this to the IO's and Streams forum.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!