new at java can i copy files or add to files

Sharky Forums


Results 1 to 4 of 4

Thread: new at java can i copy files or add to files

  1. #1
    Expensive Sushi
    Join Date
    May 2001
    Location
    Surrey, BC, Canada
    Posts
    35

    new at java can i copy files or add to files

    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

  2. #2
    Catfish arcane_III's Avatar
    Join Date
    Apr 2002
    Location
    East Coast, USA
    Posts
    151
    in the FileWriter constructor there is a boolean flag append.

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

    here is a link to the Java Online documentation.

    you can also download the documentation at this site.

  3. #3
    Expensive Sushi
    Join Date
    May 2001
    Location
    Surrey, BC, Canada
    Posts
    35
    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 is a link to the Java Online documentation.

    you can also download the documentation at this 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

  4. #4
    Tiger Shark
    Join Date
    Oct 2000
    Location
    Erie, PA, USA
    Posts
    693
    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("");
    AMD AthlonXP 2600+ Thoroughbred B @ 200x10.5
    Shuttle AN35N nForce2 Ultra 400
    2x512MB Kingston PC3200 (3-3-3)
    ATI Radeon 9600 Pro
    40GB WD ATA-100 8MB cache
    Creative 12X DVD Drive
    Memorex 52X CD-RW
    Running Windows XP Pro

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •