有些时候,要写一些程序,在 JAVA 里面好难实现, 但如果使用其它编程语言却又比较容易时,我们不妨通过 JNI 来让不同语言的程序共同完成.
JNI 的教程, 网上 C 的比较多,Java 也提供了 javah.exe 为 C 语言的 JNI 程序生成头文件, 如果你是一个 Delphi 编程员, 能否让 JAVA 与 Delphi 程序交互呢? 答案是肯定的,今天我们就来看一下一个简单的例子.
Helloworld. 主要是来认识一下, JAVA 怎样调用 Delphi 程序的方法.
好的,我们先来创建一个类:
package alvinJNI;
class HelloWorld {
static {
System.loadLibrary("DelphiAction"); //等一下我们就用Delphi来编一个程序,编好之后生成的文件就是 DelphiAction.dll 这是一个动态链接库文件,这个类里先在静态语句块中加载它
}
public native void printText(); //声明一个 native 的本地代码程序,我们使用的是 Delphi 来编写.注意:这个方法在这里只是声明,并没有定义方法体,因为这个方法我们要用 Delphi 来实现.
public static void main(String[] args) {
//创建对象并调用里面的 native 方法.
HelloWorld hw = new HelloWorld();
hw.printText();
}
}
类写完编译后,接下来的事情就由 Delphi 来解决了
我们运行 De ......
有些时候,要写一些程序,在 JAVA 里面好难实现, 但如果使用其它编程语言却又比较容易时,我们不妨通过 JNI 来让不同语言的程序共同完成.
JNI 的教程, 网上 C 的比较多,Java 也提供了 javah.exe 为 C 语言的 JNI 程序生成头文件, 如果你是一个 Delphi 编程员, 能否让 JAVA 与 Delphi 程序交互呢? 答案是肯定的,今天我们就来看一下一个简单的例子.
Helloworld. 主要是来认识一下, JAVA 怎样调用 Delphi 程序的方法.
好的,我们先来创建一个类:
package alvinJNI;
class HelloWorld {
static {
System.loadLibrary("DelphiAction"); //等一下我们就用Delphi来编一个程序,编好之后生成的文件就是 DelphiAction.dll 这是一个动态链接库文件,这个类里先在静态语句块中加载它
}
public native void printText(); //声明一个 native 的本地代码程序,我们使用的是 Delphi 来编写.注意:这个方法在这里只是声明,并没有定义方法体,因为这个方法我们要用 Delphi 来实现.
public static void main(String[] args) {
//创建对象并调用里面的 native 方法.
HelloWorld hw = new HelloWorld();
hw.printText();
}
}
类写完编译后,接下来的事情就由 Delphi 来解决了
我们运行 De ......
1.拆分字符串
遇到特殊字符,比如:对‘$’符号,就应该使用‘\\$’,后总结可以加个方括号如 "[.]"。
2.遍历HASHMAP
Iterator itr = map.keySet().itrator();
while(itr.hasNext())
{
Object temp1 = itr.next();
Object temp2 = tab.get(temp1);
}
3.日历操作
Calendar c = Canlendar.getInstance();
c.get(c.YEAR);//获取年份,其他同理
c.add(c.MONTH,-1);//上个月的日期
4.随机数
Random random = new Random();
int ran = random.nextInt(100);
注意:范围[0,100)
5.读取配置文件
对于ini 文件或者 properties文件,其实只要内容是 ds=dfjh或者 kd: ksadkf这种,就可以用
Properties pro = new Properties();
//pro.load (Main.class.getResourceAsStream("/test.properties"));或者
//pro.load (new FileInputStream ("test.properties"));
pro.getProperty ("test")
6.遍历vector
两种方式:
// for (Enumeration e = v.elements ();e.hasMoreElements ();)
// {
// & ......
1.接口的定义:
interface 接口名
{ 接口体; }
注意:接口体中只能包含常量定义和方法定义两部分;不允许提供方法的实现;其实现代码必须在其子类中实现
2.如果一个类使用了某个接口,那么这个类必须实现接口中的所有方法;而且在类中实现接口方法时,方法的名字,返回类型,参数个数
以及参数必须与接口中的完全一致
3.接口中的方法默认是public abstract方法,常量默认是public static常量,因此当类在实现接口方法时,一定要用public修饰
4.如果父类使用了某个接口,那么子类也就自然使用了该接口,子类不必再使用关键字implements声明自己使用这个接口
5.如果一个类声明实现一个接口,但没有实现接口中的所有方法,那么这个类必须是abstract类 ......
jdk提供了Zip相关的类方便的实现压缩和解压缩。使用方法很简单。下边分别是压缩和解压缩的简单事例
1,压缩的
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Zip {
static final int BUFFER = 2048;
public static void main(String argv[]) {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream("E:\\test\\myfiles.zip");
&nbs ......
JNI是Java Native Interface的缩写。从Java 1.1开始,Java Native Interface (JNI)标准成为java平台的一部分,它允许Java代码和其他语言写的代码进行交互。JNI一开始是为了本地已编译语言,尤其是C和C++而设计的,但是它并不妨碍你使用其他语言,只要调用约定受支持就可以了。
使用java与本地已编译的代码交互,通常会丧失平台可移植性。但是,有些情况下这样做是可以接受的,甚至是必须的,比如,使用一些旧的库,与硬件、操作系统进行交互,或者为了提高程序的性能。JNI标准至少保证本地代码能工作在任何Java 虚拟机实现下。
一、JNI(Java Native Interface)的设计目的
·The standard Java class library may not support the platform-dependent features needed by your application.
·You may already have a library or application written in another programming language and you wish to make it accessible to Java applications
·You may want to implement a small portion of time-critical c ......
将字串 String 转换成数字类型
String 转 int
1.) int i = Integer.parseInt([String]); 或 i = Integer.parseInt([String],[int radix]);
2.) int i = Integer.valueOf(my_str).intValue();
String 转 Float
Float f = Integer.valueOf(my_str).floatValue();
String 转 float
float f=new Float(my+str).floatValue();
注: 字串转成 Double, Float, Long 的方法大同小异.
将整数 int 转换成字串 String
1.) String s = String.valueOf(i);
2.) String s = Integer.toString(i);
3.) String s = "" + i;
注: Double, Float, Long 转成字串的方法大同小异.
JAVA中常用数据类型转换函数
虽然都能在JAVA API中找到,整理一下做个备份。
string->byte
Byte static byte parseByte(String s)
byte->string
Byte static String toString(byte b)
char->string
Character static String to String (char c)
string->Short
Short static Short parseShort(String s)
Short->String
Short static String toString(Short s)
String->Integer
Integer static int parseInt(String s)
Integer->String
Integer s ......