Example usage for java.io FileInputStream read

List of usage examples for java.io FileInputStream read

Introduction

In this page you can find the example usage for java.io FileInputStream read.

Prototype

public int read() throws IOException 

Source Link

Document

Reads a byte of data from this input stream.

Usage

From source file:Main.java

/**
 * Read the original picture to an array
 * /* w  w  w  .  j ava 2 s  . c  o m*/
 * @param initialFile
 * @param size
 * @return an array with the address and values
 */
public static int[] readBaseFile(String initialFile, int size) {
    int[] imageTxt = new int[size];
    FileInputStream fin;
    try {
        // Open an input stream
        fin = new FileInputStream(initialFile);
        int k = 0;
        while (fin.available() != 0) {
            imageTxt[k++] = fin.read();
        }
        fin.close();
    }
    // Catches any error conditions
    catch (IOException e) {
        System.err.println("Unable to read image");
        System.exit(1);
    }
    return imageTxt;
}

From source file:Main.java

public static String file2String(File file) {
    FileInputStream fis = null;
    ByteArrayOutputStream baos = null;
    try {/*from   w ww  .  j ava 2s.  c o  m*/
        fis = new FileInputStream(file);
        baos = new ByteArrayOutputStream();
        int i;
        while ((i = fis.read()) != -1) {
            baos.write(i);
        }
        String str = baos.toString();
        return str;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (fis != null)
                fis.close();
            if (baos != null)
                baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:eu.cloud4soa.c4sgitservice.utils.Util.java

public static String readfile(String filename) throws Exception {
    String ret = "";
    File file = new File(filename);
    int ch;//ww  w.j  a  v a  2s  . c o m
    StringBuffer strContent = new StringBuffer("");
    FileInputStream fin = null;
    try {
        fin = new FileInputStream(file);
        while ((ch = fin.read()) != -1)
            strContent.append((char) ch);
        fin.close();
    } catch (Exception e) {
        throw e;
    }
    ret = strContent.toString();
    return ret;
}

From source file:com.ginstr.android.service.opencellid.library.data.ApiKeyHandler.java

/**
 * Reads a random generated API key from a file stored on external storage.
 * File is located on external storage so it can be accessed from other
 * libraries.//from  ww w  .  j  a v  a2 s  . c  om
 * 
 * @return null if no key otherwise a key read from file
 */
private static String getKeyFromFile(boolean testMode) {
    String keyFilePath = testMode ? API_KEY_FILE_TEST_DEFAULT : API_KEY_FILE_DEFAULT;

    // check if the key file exists
    File keyFile = new File(keyFilePath);
    if (keyFile.exists()) {
        try {
            FileInputStream fis = new FileInputStream(keyFile);
            int ch = -1;
            StringBuffer sb = new StringBuffer();
            while ((ch = fis.read()) >= 0) {
                sb.append((char) ch);
            }
            fis.close();

            // read the key from file
            return sb.toString();
        } catch (Exception e) {
            Log.e(ApiKeyHandler.class.getSimpleName(), "Error reading key from file", e);
        }
    }

    return null;
}

From source file:gov.nist.appvet.tool.synchtest.util.ReportUtil.java

/**
 * This method returns report information to the AppVet ToolAdapter as ASCII
 * text and cannot attach a file to the response.
 *///from   ww  w.  j  a  va2s .  c  om
public static boolean sendPDFInHttpResponse(HttpServletResponse response, String reportText,
        ToolStatus reportStatus) {
    try {
        File reportFile = new File(reportFilePath);
        String mimeType = new MimetypesFileTypeMap().getContentType(reportFile);
        log.debug("Sending mimetype: " + mimeType);
        response.setContentType(mimeType);
        response.setHeader("Content-Disposition", "attachment;filename=" + reportFile.getName());
        response.setStatus(HttpServletResponse.SC_OK); // HTTP 200
        response.setHeader("apprisk", reportStatus.name());

        FileInputStream inputStream = new FileInputStream(reportFile); //read the file

        try {
            int c;
            while ((c = inputStream.read()) != -1) {
                response.getWriter().write(c);
            }
        } finally {
            if (inputStream != null)
                inputStream.close();
            response.getWriter().close();
        }

        //         PrintWriter out = response.getWriter();
        //         out.println(reportText);
        //         out.flush();
        //         out.close();
        log.info("Returned report");
        return true;
    } catch (IOException e) {
        log.error("Report not sent: " + e.toString());
        return false;
    }
}

From source file:com.pactera.edg.am.metamanager.extractor.util.AntZip.java

private static void zip(ZipOutputStream out, File f, String base) {
    try {//from  w  w  w .  j av a 2 s .com
        if (f.isDirectory()) {
            File[] fl = f.listFiles();
            if (base != null && !"".equals(base))
                out.putNextEntry(new ZipEntry(base + "/"));
            base = base.length() == 0 ? "" : base + "/";
            for (int i = 0; i < fl.length; i++) {
                zip(out, fl[i], base + fl[i].getName());
            }
        } else {
            out.putNextEntry(new ZipEntry(base));
            FileInputStream in = new FileInputStream(f);
            int b = -1;
            int buff = 0;
            while ((b = in.read()) != -1) {
                out.write(b);
                buff++;
                if (buff > 10240) {
                    buff = 0;
                    out.flush();
                }
            }
            out.flush();
            if (in != null) {
                in.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:de.mpg.escidoc.services.reporting.ReportFHI.java

protected static String readFromFile(String fileName) throws IOException {
    int ch;/*  w  w  w  . j a  va  2  s  . c om*/
    StringBuffer buff = new StringBuffer();
    FileInputStream fis = new FileInputStream(fileName);
    while ((ch = fis.read()) != -1)
        buff.append((char) ch);
    fis.close();
    return buff.toString();
}

From source file:com.clutch.ClutchAB.java

private static JSONObject getLatestMeta() {
    // TODO: Cache
    try {//ww w . j a va 2s. c  o m
        FileInputStream fis = context.openFileInput("__clutchab.json");
        StringBuffer strContent = new StringBuffer("");
        int ch;
        while ((ch = fis.read()) != -1) {
            strContent.append((char) ch);
        }
        fis.close();
        return new JSONObject(new JSONTokener(strContent.toString()));
    } catch (FileNotFoundException e) {
        return new JSONObject();
    } catch (JSONException e) {
        return new JSONObject();
    } catch (IOException e) {
        return new JSONObject();
    }
}

From source file:com.cherong.mock.common.base.util.EncryptionUtil.java

/**
 *   ??0-F?4838??/* w w w. java2s.  c  o  m*/
 * AD67EA2F3BE6E5ADD368DFE03120B5DF92A8FD8FEC2F0746  AD67EA2F3BE6E5AD
 * DES? D368DFE03120B5DF DES? 92A8FD8FEC2F0746 DES? 
 * ???".license"??
 * 
 * @param file
 * @param key
 */
public static void encrypt(File file, String key) {
    try {
        if (key.length() == 48) {
            byte[] bytK1 = string2Byte(key.substring(0, 16));
            byte[] bytK2 = string2Byte(key.substring(16, 32));
            byte[] bytK3 = string2Byte(key.substring(32, 48));

            FileInputStream fis = new FileInputStream(file);
            byte[] bytIn = new byte[(int) file.length()];
            for (int i = 0; i < file.length(); i++) {
                bytIn[i] = (byte) fis.read();
            }

            // 
            byte[] bytOut = encryptByDES(encryptByDES(encryptByDES(bytIn, bytK1), bytK2), bytK3);
            String fileOut = file.getPath() + ".license";
            FileOutputStream fos = new FileOutputStream(fileOut);
            for (int i = 0; i < bytOut.length; i++) {
                fos.write((int) bytOut[i]);
            }
            fos.close();
            fis.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.cherong.mock.common.base.util.EncryptionUtil.java

/**
 *   ??0-F?4838??/*from  w  w w.ja v  a2 s  .com*/
 * AD67EA2F3BE6E5ADD368DFE03120B5DF92A8FD8FEC2F0746  AD67EA2F3BE6E5AD
 * DES? D368DFE03120B5DF DES? 92A8FD8FEC2F0746 DES? 
 * ??
 * 
 * @param fileIn
 * @param key
 */
public static String decrypt(File fileIn, String key) {
    try {
        if (key.length() == 48) {
            String path = fileIn.getPath();
            // path = path.substring(0, path.length() - 5);

            byte[] bytK1 = string2Byte(key.substring(0, 16));
            byte[] bytK2 = string2Byte(key.substring(16, 32));
            byte[] bytK3 = string2Byte(key.substring(32, 48));

            FileInputStream fis = new FileInputStream(path);
            byte[] bytIn = new byte[(int) fileIn.length()];
            for (int i = 0; i < fileIn.length(); i++) {
                bytIn[i] = (byte) fis.read();
            }

            // 
            byte[] bytOut = decryptByDES(decryptByDES(decryptByDES(bytIn, bytK3), bytK2), bytK1);

            fis.close();

            return new String(bytOut);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}