FileWriter(.... , boolean append)

Do you see the word append in what you just posted?
Did you go the the full explanation for that version of the constructor and read what it said?

Have you tried to use that version of the constructor in your program where you want to append data to an existing file?

FileWriter("number.dat", boolean outFile);


Doesnt work.. i guess im doing it incorrectly?

When you get errors please post the full text of the error message.
"Doesnt work" does NOT say what the problem is.

When the API doc says to provide a boolean argument it means for you to code either true or false or a boolean variable.

FileWriter("number.dat", boolean outFile);
Including the variable type is wrong.
For example you coded this correctly:
File outFile = new File("numbers.dat");
vs
File outFile = new File(String "numbers.dat"); // wrong, leave off the type

FileWriter("number.dat", true);


Can you tell me what you would have done differently in this program?

Is the program now working the way you want it to?

Is the program now working the way you want it to?

Right the program at the moment just creates the file and inputs the data in it. What I was trying to do was after its created I should have the ability to close the file and run the application again where it then appends the other for loop data.

I should have the ability to close the file and run the application again where it then appends the other for loop data.

Are you saying you do not have the ability to append data to an existing file?
Even when using: FileWriter("number.dat", true);

Right it doesn't append the data at all. Have you tried out the program and played with it for a bit? You'd see its not appending it.

Please post the current version of the code you are using so we can see what your problem is.

Please post the current version of the code you are using so we can see what your problem is.

import java.io.*;
import java.util.*;
 
 
//class definition
public class NumberAdder {
 
	//The main function
	public static void main(String args[]) {
 
		//This declares a file to open.
		File outFile = new File("numbers.dat");
 
		int sumEven = 0;
		int sumOdd = 0;
 
		//File IO requires a try/catch block to prevent the program from crashing
		try {
 
			//a buffered writer is used to allow us to write to the file.
			BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));
 
			//This for loop handles adding all of the numbers together
			for(int i = 1; i < 100; i += 2) {
				sumEven += i + 1;
				writer.write("" + sumEven + ", ");
			}
 
			//adding a new line and closing the file
			writer.newLine();
			writer.close();
 
			//A buffered Reader is used for reading a new file.
			BufferedReader reader = new BufferedReader(new FileReader(outFile));
 
			//Because we only add a new line at the end, we only need to read the first line.
			System.out.println(reader.readLine());
 
			//we need to write again, so we close the reader and open the writer.
			reader.close();
			FileWriter("number.dat", true); //writer.open();
 
			//This for loop is identical to the previous one, except for odd numbers
			for(int i = 1; i < 100; i += 2) {
				sumOdd += i;
				writer.write("" + sumOdd + ", ");
			}
 
			writer.newLine();
			//writer.close();
 
			reader.open();
			System.out.println(reader.readLine());
 
		}
		catch (Exception e) {
 
		}
 
	}
}

The code you posted does not compile.
You must compile the program with out errors before you can execute it.
You must be executing an older version.

One problem I see is on line 41.
I have no idea what that code is supposed to do.
Look at line 21 to see the correct syntax for creating a new instance of the FileWriter class.

commented: Brilliantly resists to any attempt to try his patience :P +7

The code you posted does not compile.
You must compile the program with out errors before you can execute it.
You must be executing an older version.

One problem I see is on line 41.
I have no idea what that code is supposed to do.
Look at line 21 to see the correct syntax for creating a new instance of the FileWriter class.

Okay it compiles but it doesn't append... it should basically append after I run the application again.

import java.io.*;
import java.util.*;
 
 
//class definition
public class Test {
 
	//The main function
	public static void main(String args[]) {
 
		//This declares a file to open.
		File outFile = new File("numbers.dat");
 
		int sumEven = 0;
		int sumOdd = 0;
 
		//File IO requires a try/catch block to prevent the program from crashing
		try {
 
			//a buffered writer is used to allow us to write to the file.
			BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));
 
			//This for loop handles adding all of the numbers together
			for(int i = 1; i < 100; i += 2) {
				sumEven += i + 1;
				writer.write("" + sumEven + ", ");
			}
 
			//adding a new line and closing the file
			writer.newLine();
			writer.close();
 
			//A buffered Reader is used for reading a new file.
			BufferedReader reader = new BufferedReader(new FileReader(outFile));
 
			//Because we only add a new line at the end, we only need to read the first line.
			System.out.println(reader.readLine());
 
			//we need to write again, so we close the reader and open the writer.
			reader.close();
			
			BufferedWriter aWriter = new BufferedWriter(new FileWriter("number.dat", true));
 
			//This for loop is identical to the previous one, except for odd numbers
			for(int i = 1; i < 100; i += 2) {
				sumOdd += i;
				writer.write("" + sumOdd + ", ");
			}
 
			writer.newLine();
			writer.close();
 
			
			System.out.println(reader.readLine());
 
		}
		catch (Exception e) {
 
		}
 
	}
}

The problem with your code is that you used some poor techniques in your code:
1) Setting the name of your file.
You should put the name of the file you want to write to in a String and then use that String when you work with the file.
2) you left the catch block empty so you do not see any errors.

The problem with your code is that you used some poor techniques in your code:
1) Setting the name of your file.
You should put the name of the file you want to write to in a String and then use that String when you work with the file.
2) you left the catch block empty so you do not see any errors.

Okay, well let's do this. Could you pick up where I left off and show me what should have been done instead? This isn't helping me out at all since I have no idea what I did wrong.

I thought my comments were clear enough. What don't you understand about them?
Start by adding the call to printStackTrace() to the catch block.

Okay done...

import java.io.*;
import java.util.*;
 
 
//class definition
public class Test {
 
	//The main function
	public static void main(String args[]) {
 
		//This declares a file to open.
		File outFile = new File("numbers.dat");
 
		int sumEven = 0;
		int sumOdd = 0;
 
		//File IO requires a try/catch block to prevent the program from crashing
		try {
 
			//a buffered writer is used to allow us to write to the file.
			BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));
 
			//This for loop handles adding all of the numbers together
			for(int i = 1; i < 100; i += 2) {
				sumEven += i + 1;
				writer.write("" + sumEven + ", ");
			}
 
			//adding a new line and closing the file
			writer.newLine();
			writer.close();
 
			//A buffered Reader is used for reading a new file.
			BufferedReader reader = new BufferedReader(new FileReader(outFile));
 
			//Because we only add a new line at the end, we only need to read the first line.
			System.out.println(reader.readLine());
 
			//we need to write again, so we close the reader and open the writer.
			reader.close();
			
			BufferedWriter aWriter = new BufferedWriter(new FileWriter("number.dat", true));
 
			aWriter.write("number.dat");
			//This for loop is identical to the previous one, except for odd numbers
			for(int i = 1; i < 100; i += 2) {
				sumOdd += i;
				writer.write("" + sumOdd + ", ");
			}
 
			writer.newLine();
			writer.close();
			
			System.out.println(reader.readLine());
 
		}
		catch (Exception e) {
			e.printStackTrace();
		}
 
	}
}

Does it compile OK now?
What happens when you execute the program?

It compiles under console and it still doesn't append.

it still doesn't append.

What happens when it executes?
If it does not execute, it may not append.

A suggestion:
Write a very short program that opens the file in append mode, writes one word to the file and closes it. Nothing more, just that one simple thing.
Run it once and look at the output file in an editor.
Run it again and look at the output file in an editor. Do you see two copies of what was written?

Be a part of the DaniWeb community

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