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

						try	{

						

							String lines[] = taOutput.getText().split("\\n");
							for(int i = 0; i < lines.length; i++) {
								System.out.println("Value: " + i);
								BufferedWriter bw = new BufferedWriter(new FileWriter("audits.txt"));
								String s = lines[i];
								bw.write(s);
								bw.flush();
								bw.close();
							}
						}
						catch(IOException ioe) {
							System.out.println("Exception Caught : " +ioe.getMessage());
						}
					
				}
			});

		return btnSave;
	}

So my program outputs port numbers into a textarea, i wish to write these results from the textarea into a text file. I hope to write the different ports line by line into the file, so i have created the String array. So to illustrate when i run my program i may get the following:

1
22
320
1200

So instead of writing to a file and the file containing 1223201200, i have set up the array, where also i have used a System.out to display for my viewing pleasure the contents of array (well index contents). So how would i write this array to file?

Thank you.

Inside the for you do this:

BufferedWriter bw = new BufferedWriter(new FileWriter("audits.txt"));

String s = lines[i];
bw.write(s);
bw.flush();
bw.close();
}

Meaning that with each loop open the file, overwrite that was written and you close.

You need to open the file, write whatever you want and when you are done close it:

BufferedWriter bw = new BufferedWriter(new FileWriter("audits.txt"));

for (int i=0;i<lines.length;i++) {
  String s = lines[i];
  bw.write(s);
  bw.newLine();
}

bw.flush();
bw.close();

So for my purposes of understanding this, my error in my ways was that i was closing the stream before i had finished writing, so technically it was outputting the results correctly but only processing one line to file?

Thank you by the way javaAddict and yep i checked out your bike too in the process. You have to make sure she stays royal that one; will get a fair bit attention.

So for my purposes of understanding this, my error in my ways was that i was closing the stream before i had finished writing, so technically it was outputting the results correctly but only processing one line to file?

Thank you by the way javaAddict and yep i checked out your bike too in the process. You have to make sure she stays royal that one; will get a fair bit attention.

I assume that your problem has been fixed. Do you get the results that you want?

This question has already been answered. Start a new discussion instead.