Click to See Complete Forum and Search --> : new at java can i copy files or add to files


Triple__J
04-12-2002, 12:22 PM
Hi I wrote this but not sure how to get it to add to the new.txt is there a way to do that.

import java.io.*;

public class Copy
{
public static void main(String[] args) throws IOException
{
File inputFile = new File("test.txt");
File outputFile = new File("new.txt");

FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;

while ((c = in.read()) != -1)
out.write(c);

in.close();
out.close();
}
}

just not to sure if it can be done or if the book I'm looking at has that info. Thanks

arcane_III
04-12-2002, 12:48 PM
in the FileWriter constructor there is a boolean flag append.

just change your code to:
FileWriter out = new FileWriter(outputFile,true);

here (http://java.sun.com/docs/index.html) is a link to the Java Online documentation.

you can also download the documentation at this (http://java.sun.com/j2se/1.4/download.html#docs) site.

Triple__J
04-12-2002, 01:42 PM
Originally posted by arcane_III
in the FileWriter constructor there is a boolean flag append.

just change your code to:
FileWriter out = new FileWriter(outputFile,true);

here (http://java.sun.com/docs/index.html) is a link to the Java Online documentation.

you can also download the documentation at this (http://java.sun.com/j2se/1.4/download.html#docs) site.
tried that but get the this error
Incompatible type for constructor. Can't convert java.io.File to java.lang.String.
FileWriter out = new FileWriter(outputFile,true);

do i just add java.lang.String

Malone
04-12-2002, 02:24 PM
Check the documentation on that, I know a bit about java but not much about I/O and creating files. Anyway, it looks like the FileWriter constructor takes a String as it's first argument, and you have the outputFile variable declared as a File, not a String. Hence the incompatible type error.

Try declaring outputFile as a String, just to see what happens. In fact get rid of the whole declaration cause it looks like you don't really need it. Then just construct a FileMaker object wiht an empty String.

FileMaker out = new FileMaker("");