tags:

views:

2117

answers:

5

Hi- I would like to write a function copy(File f1, File f2) f1 is always a file. f2 is either a file or a directory.

If f2 is a directory I would like to copy f1 to this directory (the file name should stay the same).

If f2 is a file I would like to copy the contents of f1 to the end of the file f2.

So for example if F2 has the contents:

2222222222222

And F1 has the contents

1111111111111

And I do copy(f1,f2) then f2 should become

2222222222222

1111111111111

Thanks!

A: 

Check out the link below. it is a source file that copies a file to another using NIO.

http://www.java2s.com/Code/Java/File-Input-Output/CopyafileusingNIO.htm

Mr. Will
+2  A: 

FileOutputStream has a constructor that allows you to specify append as opposed to overwrite.

You can use this to copy the contents of f1 to the end of f2 as below:

  File f1 = new File(srFile);
  File f2 = new File(dtFile);

  InputStream in = new FileInputStream(f1);
  OutputStream out = new FileOutputStream(f2, true);

  byte[] buf = new byte[1024];
  int len;
  while ((len = in.read(buf)) > 0){
    out.write(buf, 0, len);
  }
  in.close();
  out.close();
Allain Lalonde
+4  A: 

Apache Commons IO to the rescue!

Expanding on Allain's post:

  File f1 = new File(srFile);
  File f2 = new File(dtFile);

  InputStream in = new FileInputStream(f1);
  OutputStream out = new FileOutputStream(f2, true); // appending output stream

  try {
     IOUtils.copy(in, out);
  }
  finally {
      IOUtils.closeQuietly(in);
      IOUtils.closeQuietly(out);
  }

Using Commons IO can simplify a lot of the stream grunt work.

Elijah
+2  A: 

Using the code from the answer by Allain Lolande and extending it, this should address both parts of your question:

File f1 = new File(srFile);
File f2 = new File(dtFile);

// Determine if a new file should be created in the target directory,
// or if this is an existing file that should be appended to.
boolean append;
if (f2.isDirectory()) {
    f2 = new File(f2, f1.getName());
    // Do not append to the file. Create it in the directory, 
    // or overwrite if it exists in that directory.
    // Change this logic to suite your requirements.
    append = false;
} else {
    // The target is (likely) a file. Attempt to append to it.
    append = true;
}

InputStream in = null;
OutputStream out = null;
try {
    in = new FileInputStream(f1);
    out = new FileOutputStream(f2, append);

    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
} finally {
    if (out != null) {
        out.close();
    }
    if (in != null) {
        in.close();
    }
}
Jared Oberhaus
A: 

I would like to delete a string or a line inside a ".txt" file ( i.e. filen.txt ). For example, I have these lines in the file:

1JUAN DELACRUZ
2jUan dela Cruz
3Juan Dela Cruz

then delete the 2nd line (2jUan dela Cruz), so the ".txt" file will look like:

1JUAN DELACRUZ
3Juan Dela Cruz

Please help.Thank you

raya lama
You should really ask this as a separate question, this is not an answer to this question here. Or better just search for it, this surely has come up before.
sth