Java 调用Native Method和Libary,就会丢失Java自己的一些属性,比如夸平台运行,除非你确认必须调用Native Method 和Library,否则尽量别用,我们通常这样用的原因是之前我们写了很多代码,希望重用,或者是因为执行速度的原因我们需要利用Native code去运行特定的功能。
1. 调用Native Method和Library的例子,
public class SimpleFile {
public static final char separatorChar='>';
protected String path;
protected int fd;
public SimpleFile(String path) {
// TODO Auto-generated constructor stub
this.path = path;
}
public String getFileName()
{
int index = path.lastIndexOf(separatorChar);
return index <0? path: path.substring(index +1);
}
public String getPath()
{
return this.path;
}
public native boolean open();
public native void close();
public native int read(byte[] buffer, int length);
public native int write(byte[] buffer, int length);
// run when the class is loaded first time.
static {
System.loadLibrary("Simple"); //simple.dll