Use waitfor for process java example

                    p = Runtime.getRuntime().exec(“your command”);                     StreamGobbler errorGobbler = new                     StreamGobbler(p.getErrorStream(), “ERROR”);                     StreamGobbler outputGobbler = new                     StreamGobbler(p.getInputStream(), “OUTPUT”);                     errorGobbler.start();                     outputGobbler.start();                     int exitCode = p.waitFor();                     if(exitCode==0){                            result = true;                     } else {                            String out = “”;                            […]

Read More »

GZIPInputStream java example

If you have a gzip file and you want decompress it you can use this code :   byte[] buffer = new byte[1024]; GZIPInputStream gzis = null; FileOutputStream out = null;      try{          gzis = new GZIPInputStream(new FileInputStream(INPUT_GZIP_FILE));          out =   new FileOutputStream(OUTPUT_FILE);         int len;         while ((len […]

Read More »

Get file’s mime-type

To get file’s mime-type I usually use tika-app-1.3.jar library. You can download it here . In this way you can use the tika library to obtein the mime-type.   public static String getMimeFromFialeTika(String nomeFile ) throws Exception {        InputStream fileStream = null ;        org.apache.tika.mime.MediaType mediaType = null ;        try {              File file = […]

Read More »

Count wornds  java example

public static int countWords(String str, String findStr){     int lastIndex = 0;     int count =0;     while(lastIndex != -1){            lastIndex = str.indexOf(findStr,lastIndex);            if( lastIndex != -1){                  count ++;            }            if (lastIndex==-1) break;            lastIndex+=findStr.length();     }     return count ; }

Read More »

Move file java example

public static void moveFiles(File source, File dest) {              InputStream inStream = null;              OutputStream outStream = null;              try{                     inStream = new FileInputStream(source);                     outStream = new FileOutputStream(dest);                     byte[] buffer = new byte[1024];                     int length;                     while ((length = inStream.read(buffer)) > 0){                            outStream.write(buffer, 0, length);                     }                     inStream.close();                     […]

Read More »

Copy file java example

public static void copyFiles(File source, File dest) throws IOException {           InputStream input = null;           OutputStream output = null;           try {                 input = new FileInputStream(source);                 output = new FileOutputStream(dest);                 byte[] buf = new byte[1024];                 int bytesRead;                 while ((bytesRead = input.read(buf)) […]

Read More »

Encode/Decode Base64 to byte[]

         public static byte[] encode(byte[] b) throws Exception {            ByteArrayOutputStream baos = new ByteArrayOutputStream();            OutputStream b64os = MimeUtility.encode(baos, “base64”);            b64os.write(b);            b64os.close();            return baos.toByteArray();          }            public static byte[] decode(byte[] b) throws Exception {            ByteArrayInputStream bais = […]

Read More »

Get hash-256 from file into base64

String file_path = “your file from your file system “; System.out.println(“File to upload ” + file_path); FileInputStream fis = null; try {   File file = new File(file_path);   if (!file.exists() || file.length() == 0) {       Utils.stampaLogE(“Conversion Hash SHA-256 . Bad input !!! ( File not found / File empty)…”);       throw new RuntimeException(“Conversion […]

Read More »

Show Buttons
Hide Buttons