Thursday, September 15, 2011

md5 in java whitout reading file in a buffer


import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Scanner;


public class MD5 {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String[] cmd = new String[]{"md5sum","/home/user/workspace/project/buildfile"};
ProcessBuilder pb = new ProcessBuilder(cmd);
Process p = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
String answer = sb.toString();

int val = p.waitFor();
String md5 ="";
if(val == 0){
Scanner s = new Scanner(answer);
if(s.hasNext()){
md5 = s.next();
System.out.println(md5);
}
}else{
System.out.println("Error executing command md5sum");
}

}


}


No comments:

Post a Comment