// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.*;

public class CopyManyWays {
    public static void main(String[] args) {
        copy1("c:\\tst0.fil", "c:\\tst1.fil");
        copy2("c:\\tst0.fil", "c:\\tst2.fil");
        copy3("c:\\tst0.fil", "c:\\tst3.fil");
    }
    public static void copy1(String fnm1, String fnm2) {
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fnm1));
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fnm2));
            byte[] b = new byte[1000];
            int n;
            while((n = bis.read(b)) >= 0) {
                bos.write(b, 0, n);
            }
            bis.close();
            bos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return;
    }
    public static void copy2(String fnm1, String fnm2) {
        File f = new File(fnm1);
        int n = (int)f.length();
        byte[] b = new byte[n];
        try {
            InputStream is = new FileInputStream(fnm1);
            OutputStream os = new FileOutputStream(fnm2);
            is.read(b);
            os.write(b);
            is.close();
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return;
    }
    public static void copy3(String fnm1, String fnm2) {
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fnm1));
            OutputStream os = new FileOutputStream(fnm2);
            byte[] b = new byte[1000];
            int n;
            StringBuffer sb = new StringBuffer();
            while((n = bis.read(b)) >= 0) {
                sb.append(new String(b, 0, n));
            }
            bis.close();
            os.write(sb.toString().getBytes());
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return;
    }
}
