0

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?

0

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


Doesnt work.. i guess im doing it incorrectly?

0

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

0

FileWriter("number.dat", true);


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

0

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.

0

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);

0

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.

0

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

0

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) {
 
		}
 
	}
}
1

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.

Votes + Comments
Brilliantly resists to any attempt to try his patience :P
0

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) {
 
		}
 
	}
}
0

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.

0

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.

0

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.

0

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();
		}
 
	}
}
0

it still doesn't append.

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

0

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?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.