How to append to a file?

I am writing a program and I need it to do one of two things at start up

1. Check to see if the file "shopping.dat" exists, if it does, then leave it alone

2. If it does not exist, then create a file called "shopping.dat"

I understand its not creating a file, persae, but really an outputstream that writes to the specified file name. The only code I have so far is this:

PrintWriter outputStream =null;

try{

outputStream =new PrintWriter(new FileOutputStream("ShoppingCart.dat"));

}catch (FileNotFoundException e){

e.printStackTrace();

}

All help much appreciated :)

[933 byte] By [eristica] at [2007-11-27 8:41:05]
# 1
Have a look at the documentation: http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html[edit]Check the .exists() method in particular. ;-)Message was edited by: Navy_Coder
Navy_Codera at 2007-7-12 20:39:53 > top of Java-index,Java Essentials,New To Java...
# 2

Okay, I got that. Now how do I go about telling it to append to the existing file if it exists ?

PrintWriter outputStream = null;

try {

File file = new File("ShoppingCart.dat");

boolean exist = file.exists();

if (exist != true) {

outputStream = new PrintWriter(new FileOutputStream("ShoppingCart.dat"));

else {

?

}

} catch (FileNotFoundException e) {

e.printStackTrace();

}

eristica at 2007-7-12 20:39:53 > top of Java-index,Java Essentials,New To Java...
# 3
From the java.io API:FileOutputStream(File file, boolean append) Creates a file output stream to write to the file represented by the specified File object.
LazarusLonga at 2007-7-12 20:39:53 > top of Java-index,Java Essentials,New To Java...
# 4

Okay. This program needs to write to an array. So, I have it writing to an array list as outputStream (items). Items is a bunch of data about what people are buying in a store.

I am just trying to have it check to see if the file "ShoppingCart.dat" exists. If it does, then append else, create a new outputStream to write to "ShoppingCart.dat"

The problem I'm running into is that it says duplicate variable name. Since the customers info is writing to an array list, and I'm printing to it as outputStream.println(items). What can I do ?

try {

File file = new File("ShoppingCart.dat");

boolean exist = file.exists();

if (exist = true) {

FileOutputStream outputStream

= new FileOutputStream ("ShoppingCart.dat", true);

}

else {

outputStream

= new PrintWriter(new FileOutputStream("ShoppingCart.dat"));

}

} catch (FileNotFoundException e) {

e.printStackTrace();

}

eristica at 2007-7-12 20:39:53 > top of Java-index,Java Essentials,New To Java...
# 5

Fixed it with this...

try {

outputStream = new PrintWriter(

new BufferedWriter(new FileWriter("ShoppingCart.dat", true)));

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

System.err.println(e.toString());

}

It appends if the file exists, and creates the output stream if it doesn't.

eristica at 2007-7-12 20:39:53 > top of Java-index,Java Essentials,New To Java...
# 6

I believe

else {

outputStream

= new PrintWriter(new FileOutputStream("ShoppingCart.dat"));

should read

else {

FileOutputStream outputStream

= new PrintWriter(new FileOutputStream("ShoppingCart.dat"));

In any case, I'm not sure you need an if statement.

LazarusLonga at 2007-7-12 20:39:53 > top of Java-index,Java Essentials,New To Java...
# 7

FileWriter fw = new FileWriter(myFile, true); // the true parameter tells it to append.

Here's the API documentation:

http://java.sun.com/j2se/1.5.0/docs/api/java/io/FileWriter.html

Take a close look at the constructors.

Navy_Codera at 2007-7-12 20:39:53 > top of Java-index,Java Essentials,New To Java...