JAVA文件拷贝(Windows & Linux)

package unsorted;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class CopyFile {
    
    private static String osname = System.getProperties().getProperty("os.name");
    
    public static void copyfile() {
        
        if (osname.contains("Windows")) {
            
            File f = new File("C:/Windows/System32/jnetpcap.dll");
            if(! f.exists()) {
                System.out.println("copy jnetpcap.dll to system32 folder");
                
                InputStream instream = CopyFile.class.getResourceAsStream("/jnetpcap.dll");
                OutputStream out = null;
                try {
                    out = new FileOutputStream(new File("C/Windows/System32/jnetpcap.dll"));
                    int read = 0;
                    byte[] bytes = new byte[2048];
                    while((read = instream.read(bytes)) != -1) {
                        out.write(bytes, 0, read);
                    }
                    out.flush();
                    instream.close();
                    out.close();
                }
                catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("copy jnetpcap.dll failed");
                }
                
            }
        }
        else if(osname.equalsIgnoreCase("Linux")) {
            
            File f = new File("/usr/lib/libjnetpcap.so");
            if(! f.exists()) {
                System.out.println("copy libjnetpcap.so to /usr/lib/ folder");
                
                InputStream instream = CopyFile.class.getResourceAsStream("/libjnetpcap.so");
                OutputStream out = null;
                try {
                    out = new FileOutputStream(new File("/usr/lib/jnetpcap.so"));
                    int read = 0;
                    byte[] bytes = new byte[2048];
                    while((read = instream.read(bytes)) != -1) {
                        out.write(bytes, 0, read);
                    }
                    out.flush();
                    instream.close();
                    out.close();
                }
                catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("copy jnetpcap.so failed");
                }
            }
        }
    }

}

 

posted on 2014-11-19 22:18  starzou  阅读(1748)  评论(0编辑  收藏  举报

导航