Java String to Byte Array getBytes(String outputFile, Map queries)

Here you can find the source of getBytes(String outputFile, Map queries)

Description

get Bytes

License

Open Source License

Declaration

public static byte[] getBytes(String outputFile, Map<String, String> queries) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;

import java.util.Map;

public class Main {

    public static byte[] getBytes(String outputFile, Map<String, String> queries) {

        FileInputStream in = null;
        try {//from ww w  .j a v a  2  s  .  c o  m
            in = new FileInputStream(outputFile);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            int c;
            byte buffer[] = new byte[1024];
            while ((c = in.read(buffer)) != -1) {
                for (int i = 0; i < c; i++)
                    out.write(buffer[i]);
            }
            out.close();
            if (queries != null && queries.size() > 0) {
                String content = out.toString("utf-8");
                for (String key : queries.keySet()) {
                    content = content.replaceAll("%" + key + "%", queries.get(key));
                }
                return content.getBytes("utf-8");
            }
            return out.toByteArray();
        } catch (Exception ex) {
            System.err.println(ex.getMessage());
            return new byte[0];
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }
    }
}

Related

  1. convertStringToByteArray(String input)
  2. convertStringToByteArray(String string)
  3. convertStringToBytes(String string)
  4. getBytes(String k)
  5. getBytes(String k)
  6. getBytes(String s)
  7. getBytes(String s)
  8. getBytes(String s)
  9. getBytes(String s)