List of usage examples for java.io FileInputStream read
public int read(byte b[]) throws IOException
b.length
bytes of data from this input stream into an array of bytes. From source file:Main.java
static private void addToZip(String path, String srcFile, ZipOutputStream zip) throws IOException { File folder = new File(srcFile); if (folder.isDirectory()) { addFolderToZip(path, srcFile, zip, true); } else {/*w w w .ja v a 2s .c o m*/ byte[] buf = new byte[1024]; int len; FileInputStream in = null; try { in = new FileInputStream(srcFile); zip.putNextEntry(new ZipEntry(path + File.separator + folder.getName())); while ((len = in.read(buf)) > 0) { zip.write(buf, 0, len); } } finally { if (in != null) try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:Main.java
public static byte[] getByteArray(File filePath, String name) { FileInputStream fileInputStream = null; File file = new File(filePath, name); byte[] bFile = new byte[(int) file.length()]; try {/*from w ww . j av a 2 s . c o m*/ //convert file into array of bytes fileInputStream = new FileInputStream(file); fileInputStream.read(bFile); fileInputStream.close(); } catch (Exception e) { e.printStackTrace(); } return bFile; }
From source file:FileUtil.java
/** * Zip up a directory path/*from www.j a va2s . co m*/ * @param directory * @param zos * @param path * @throws IOException */ public static void zipDir(String directory, ZipOutputStream zos, String path) throws IOException { File zipDir = new File(directory); // get a listing of the directory content String[] dirList = zipDir.list(); byte[] readBuffer = new byte[2156]; int bytesIn = 0; // loop through dirList, and zip the files for (int i = 0; i < dirList.length; i++) { File f = new File(zipDir, dirList[i]); if (f.isDirectory()) { String filePath = f.getPath(); zipDir(filePath, zos, path + f.getName() + "/"); continue; } FileInputStream fis = new FileInputStream(f); try { ZipEntry anEntry = new ZipEntry(path + f.getName()); zos.putNextEntry(anEntry); bytesIn = fis.read(readBuffer); while (bytesIn != -1) { zos.write(readBuffer, 0, bytesIn); bytesIn = fis.read(readBuffer); } } finally { fis.close(); } } }
From source file:Main.java
public static void divideFile() throws IOException { String path = "F:/ar/"; String base = "phoneAttribution"; String ext = ".db"; int split = 800 * 1024; byte[] buf = new byte[1024]; int num = 1;/*from w w w . j a v a2 s . c o m*/ File inFile = new File(path + base + ext); FileInputStream fis = new FileInputStream(inFile); while (true) { FileOutputStream fos = new FileOutputStream(new File(path + base + num + ext)); for (int i = 0; i < split / buf.length; i++) { int read = fis.read(buf); fos.write(buf, 0, buf.length); if (read < buf.length) { fis.close(); fos.close(); return; } } fos.close(); num++; } }
From source file:Main.java
public static byte[] createMD5(FileInputStream is) throws IOException { MessageDigest digester = null; try {/*from w w w . ja v a 2 s. c o m*/ digester = MessageDigest.getInstance("MD5"); byte[] buffer = new byte[16 * 1024]; // 16k int length = 0; while ((length = is.read(buffer)) > 0) { digester.update(buffer, 0, length); } } catch (Exception e) { e.printStackTrace(); } finally { is.close(); } if (digester != null) { return digester.digest(); } return null; }
From source file:Main.java
public static void post(String actionUrl, String file) { try {/*from www . ja va2 s . c o m*/ URL url = new URL(actionUrl); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); con.setRequestMethod("POST"); con.setRequestProperty("Charset", "UTF-8"); con.setRequestProperty("Content-Type", "multipart/form-data;boundary=*****"); DataOutputStream ds = new DataOutputStream(con.getOutputStream()); FileInputStream fStream = new FileInputStream(file); int bufferSize = 1024; // 1MB byte[] buffer = new byte[bufferSize]; int bufferLength = 0; int length; while ((length = fStream.read(buffer)) != -1) { bufferLength = bufferLength + 1; ds.write(buffer, 0, length); } fStream.close(); ds.flush(); InputStream is = con.getInputStream(); int ch; StringBuilder b = new StringBuilder(); while ((ch = is.read()) != -1) { b.append((char) ch); } new String(b.toString().getBytes("ISO-8859-1"), "utf-8"); ds.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.fredhopper.core.connector.index.FileUtils.java
public static void addToZipFile(final File file, final ZipOutputStream zos) throws IOException { final FileInputStream fis = new FileInputStream(file); final ZipEntry zipEntry = new ZipEntry(file.getName()); zos.putNextEntry(zipEntry);/*from w w w . j a va 2 s .com*/ final byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { zos.write(bytes, 0, length); } zos.closeEntry(); fis.close(); }
From source file:Main.java
@SuppressWarnings("resource") public static boolean isSameContent(String srcFilename, String destFilename) { try {//from w ww . j a va 2s . c o m FileInputStream src = new FileInputStream(srcFilename); FileInputStream dst = new FileInputStream(destFilename); byte[] bsrc = new byte[1024]; byte[] bdst = new byte[1024]; int n; while ((n = src.read(bsrc)) != -1) { int m = dst.read(bdst); if (m == -1) return false; if (m != n) return false; if (!Arrays.equals(bsrc, bdst)) return false; } return true; } catch (Exception e) { return false; } }
From source file:Main.java
public static byte[] getFileSHA1(String path) throws IOException { File file = new File(path); FileInputStream in = new FileInputStream(file); MessageDigest messagedigest;/*from www.ja va 2 s.co m*/ try { messagedigest = MessageDigest.getInstance("SHA-1"); byte[] buffer = new byte[1024 * 64]; int len; while ((len = in.read(buffer)) > 0) { messagedigest.update(buffer, 0, len); } return messagedigest.digest(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (OutOfMemoryError e) { e.printStackTrace(); throw e; } finally { in.close(); } return null; }
From source file:Main.java
public static String getFileMD5String(File file) { if (!file.exists() || file.isFile()) { return null; }// www. jav a 2 s . c om MessageDigest messagedigest; FileInputStream fis = null; try { fis = new FileInputStream(file); messagedigest = MessageDigest.getInstance("MD5"); int len; byte[] buf = new byte[1024]; while ((len = fis.read(buf)) != -1) { messagedigest.update(buf, 0, len); } byte md5Bytes[] = messagedigest.digest(); StringBuffer stringbuffer = new StringBuffer(); for (int i = 0; i < md5Bytes.length; i++) { stringbuffer.append(HEX_DIGITS[(md5Bytes[i] & 0xf0) >>> 4]); stringbuffer.append(HEX_DIGITS[md5Bytes[i] & 0x0f]); } return stringbuffer.toString(); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }