我有个C++写的程序,其功能是生成一个文件,我的java程序是调用c的执行程序,生成文件,然后读取这个文件,读取的文件内容是null,打开文件也是null,但如果关闭程序打开文件确有内容代码如下:
package com.itelgent.util;import java.io.*;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: itelgent</p>
*
* @author zhanglu
* @version 1.0
*/
class CpuIdentity {
// static {
// System.loadLibrary("getCUPID"); //载入静态库(系统程序库),getCpuId函数在其中实现
// } //private native int get_encode_hwid(char id[], int len,CPUID c); //声明本地调用// private native String getid(); //声明本地调用 public static boolean makefile(String cmd){
boolean flag=true;
try {
Runtime run=Runtime.getRuntime();
run.exec(cmd);
run.gc();
run.halt(0);
flag=true;
}
catch (IOException ex) {
flag=false;
}
return flag;
} public static String getIdString(String exe,String filename) {
String s=null;
String cmd=exe+" "+filename;
if(makefile(cmd)){
try {
File file = new File(filename);
if (file.exists()) {
FileInputStream fin = new FileInputStream(file);
ByteArrayOutputStream out =new ByteArrayOutputStream();
byte[] bb=new byte[100];
int len=0;
while((len=fin.read(bb))>0){
if(len==100){
out.write(bb);
}else{
byte[] aa=new byte[len];
System.arraycopy(bb,0,aa,0,len);
out.write(aa);
}
}
s = new String(out.toByteArray());
}
else {
makefile(cmd);
}
}
catch (Exception ex1) {
}
}
return s;
} public static void main(String[] args) {
System.out.println(CpuIdentity.getIdString("./hwid.sh","id.txt"));
}
}
package com.itelgent.util;import java.io.*;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: itelgent</p>
*
* @author zhanglu
* @version 1.0
*/
class CpuIdentity {
// static {
// System.loadLibrary("getCUPID"); //载入静态库(系统程序库),getCpuId函数在其中实现
// } //private native int get_encode_hwid(char id[], int len,CPUID c); //声明本地调用// private native String getid(); //声明本地调用 public static boolean makefile(String cmd){
boolean flag=true;
try {
Runtime run=Runtime.getRuntime();
run.exec(cmd);
run.gc();
run.halt(0);
flag=true;
}
catch (IOException ex) {
flag=false;
}
return flag;
} public static String getIdString(String exe,String filename) {
String s=null;
String cmd=exe+" "+filename;
if(makefile(cmd)){
try {
File file = new File(filename);
if (file.exists()) {
FileInputStream fin = new FileInputStream(file);
ByteArrayOutputStream out =new ByteArrayOutputStream();
byte[] bb=new byte[100];
int len=0;
while((len=fin.read(bb))>0){
if(len==100){
out.write(bb);
}else{
byte[] aa=new byte[len];
System.arraycopy(bb,0,aa,0,len);
out.write(aa);
}
}
s = new String(out.toByteArray());
}
else {
makefile(cmd);
}
}
catch (Exception ex1) {
}
}
return s;
} public static void main(String[] args) {
System.out.println(CpuIdentity.getIdString("./hwid.sh","id.txt"));
}
}
run.exec(cmd);
run.waitFor();
...
run.exec(cmd);
run.gc();
run.halt(0);
修改成这样 Process run=Runtime.getRuntime().exec(cmd);
//其中参数cmd是A specified system command。一个具体的系统命令。
另外你还要把你程序中的一这句话System.out.println(CpuIdentity.getIdString("./hwid.sh","id.txt"));
改成System.out.println(CpuIdentity.getIdString("notepad.exe","id.txt"));
可能你的OS是unix,我的是windows。
我把你的原程序已经修改好,我的EMAIL是[email protected]
你给我发邮件,我把原程序发给你。