首页 > 编程语言 > 详细

JAVA文件拷贝(Windows & Linux)

时间:2014-11-19 23:35:54      阅读:282      评论:0      收藏:0      [点我收藏+]
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");
                }
            }
        }
    }

}

 

JAVA文件拷贝(Windows & Linux)

原文:http://www.cnblogs.com/starzou/p/4109347.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!