Member Avatar

Hello developers, i have got a problem that i have been trying to solve all day and would be glad if anyone here can help.

I have a Java port scanning application which scans for open ports and displays the results in a textarea called "taOutput" when the user clicks on the "Save" button.

Here is my code:

private JButton getBtnSave() {
		if(btnSave == null) {
			btnSave = new JButton();
			btnSave.setToolTipText("Save scan output");
			btnSave.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {

						try	{

							String s = taOutput.getText();

							File f = new File("audits.txt");
							FileWriter fw = new FileWriter(f);
						catch(IOException ioe) {
							System.out.println("Exception Caught : " +ioe.getMessage());
		return btnSave;

Problem i have is that when i click on the "Save" button the port scan results are not written to the text file i have specified. Can anybody sense what is missing here?

Kind regards


Recommended Answers

All 8 Replies

maybe i need to see more code but i dont see why you are creating your JButton in a method that returns a JButton.

private JButton getBtnSave() {

wouldnt you create the button in the panel constructor or intialize components? and what is this if(btnSave == null) {

it has to hit that to create the button and action listener. I would be looking to see if your action listener is being called.

Also this should be stand alone, i dont think applets can write to files.


Member Avatar

Hello Mike,

What i have is a JFrame i am intializing. I had a revisison of my code and have removed the non-required if(btnSave == null) { line of code.

I did have a SaveDialog which was intializing and creating the preset file so i know the actionlistener is responding to the action command. I have removed the dialog for now though to sort out my situation.

Also to note, the return btnSave is required by the ActionListener.

I am wondering if this has anything to do with how i am marshalling the data or anything like that because i have also tried the below code to, with no avail:

//BufferedWriter bw = new BufferedWriter(new FileWriter("audits.txt"));
							//String t = s.toString();
							//String auditFile = null;

Commented out in my code for now though.

after you cal write() you typically call close. maybe not calling close it not garanteeing things get written in the end. also you have a flush method available to clean the pipes and make sure its all written. someone said on the web that calling close also invokes flush. so maybe not having close is the problem.


or just 
fw.close(); // probably works no flush needed


Member Avatar

My word, i totally forgot about that. My past Java degree lecturer rung that down my neck.

It is required to call flush and close methods for the pipes etc, i have tried this and it is working, i.e performs the write to file.

So i kindly thank you for reminding me and also pointing out the vital elements i require, so thank you Mike.

cool i like happy ending.

mark as solved! i only solved 4 threads but i think its more :)


Member Avatar

Oh so i have that ability then, consider this a happy ending.

May the threads be with you.

Hello divya, welcome to DaniWeb

Please do not hijack old threads with your new question. I have deleted your post so you can create your own new thread. You may also want to post the relevant code - it's hard for someone to say what you did wrong if they don't know anything about what you did


Please do not hijack old threads with your new question. I have deleted your post so you can create your own new thread. This thread has been solved (a year ago) and you are unlikely to get any help by asking a completely different question in an already answered thread about something else...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.