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
/** * Converts a file specified by the path, to the String. *//*from w w w . j a va 2s .c om*/ public static String fileToString(String fileName) { if (fileName != null) { //String sLine; byte[] utf8Bytes; String sFile = new String(); // Reading input by lines: try { FileInputStream fis = new FileInputStream(fileName); int noOfBytes = fis.available(); if (noOfBytes > 0) { utf8Bytes = new byte[noOfBytes]; fis.read(utf8Bytes); sFile = new String(utf8Bytes, "UTF8"); } } catch (Exception ex) { return null; } return sFile; } return null; }
From source file:core.SHA256Hash.java
public static String CalculateCheckSum(String FileName, String url) throws Exception { MessageDigest md = MessageDigest.getInstance(getHashAlgType(url)); FileInputStream fis = new FileInputStream(FileName); byte[] dataBytes = new byte[1024]; int nread = 0; while ((nread = fis.read(dataBytes)) != -1) { md.update(dataBytes, 0, nread);// www . jav a 2 s . co m } ; byte[] mdbytes = md.digest(); // Store byte array as string and return it String value = new String(mdbytes); System.out.println("Encoded " + getHashAlgType(url) + " hash: " + value); return value; }
From source file:Main.java
/** * Reads content of the file into string buffer * @param tempFile instance of java.io.File that points to an actual file on the file system. * @return instance of java.lang.StringBuffer *///from w w w. j a v a 2 s . co m public static StringBuffer readFromFile(File tempFile) { StringBuffer stringBuffer = new StringBuffer(); FileInputStream is = null; try { is = new FileInputStream(tempFile); byte[] buffer = new byte[1024]; int count = 0; while ((count = is.read(buffer)) != -1) { stringBuffer.append(new String(buffer, 0, count)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } return stringBuffer; }
From source file:eu.stratosphere.nephele.jobmanager.web.LogfileInfoServlet.java
private static void writeFile(OutputStream out, File file) throws IOException { byte[] buf = new byte[4 * 1024]; // 4K buffer FileInputStream is = null; try {/*from w w w .j a v a 2s. c om*/ is = new FileInputStream(file); int bytesRead; while ((bytesRead = is.read(buf)) != -1) { out.write(buf, 0, bytesRead); } } finally { if (is != null) { is.close(); } } }
From source file:com.recomdata.transmart.data.export.util.ZipUtil.java
/** * This method will bundle all the files into a zip file. * If there are 2 files with the same name, only the first file is part of the zip. * /*from w w w .java2 s . c om*/ * @param zipFileName * @param files * * @return zipFile absolute path * */ public static String bundleZipFile(String zipFileName, List<File> files) { File zipFile = null; Map<String, File> filesMap = new HashMap<String, File>(); if (StringUtils.isEmpty(zipFileName)) return null; try { zipFile = new File(zipFileName); if (zipFile.exists() && zipFile.isFile() && zipFile.delete()) { zipFile = new File(zipFileName); } ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile)); zipOut.setLevel(ZipOutputStream.DEFLATED); byte[] buffer = new byte[BUFFER_SIZE]; for (File file : files) { if (filesMap.containsKey(file.getName())) { continue; } else if (file.exists() && file.canRead()) { filesMap.put(file.getName(), file); zipOut.putNextEntry(new ZipEntry(file.getName())); FileInputStream fis = new FileInputStream(file); int bytesRead; while ((bytesRead = fis.read(buffer)) != -1) { zipOut.write(buffer, 0, bytesRead); } zipOut.flush(); zipOut.closeEntry(); } } zipOut.finish(); zipOut.close(); } catch (IOException e) { //log.error("Error while creating Zip file"); } return (null != zipFile) ? zipFile.getAbsolutePath() : null; }
From source file:cz.cas.lib.proarc.common.imports.InputUtils.java
/** * Checks a file for magic numbers.//www. j av a2 s . c om * @param f file to check * @param magics list of magic numbers * @return {@code true} if the file starts with some of magic numbers. * @throws IOException file access failure */ public static boolean hasMagicNumber(File f, byte[]... magics) throws IOException { int maxlength = 0; int minlength = 0; for (byte[] magic : magics) { maxlength = Math.max(maxlength, magic.length); minlength = Math.min(minlength, magic.length); } FileInputStream fis = new FileInputStream(f); try { byte[] buf = new byte[maxlength]; int length = fis.read(buf); if (length < minlength) { return false; } for (byte[] magic : magics) { if (startWith(buf, length, magic)) { return true; } } return false; } finally { IOUtils.closeQuietly(fis); } }
From source file:Main.java
public static byte[] fileToByte(String filePath) throws Exception { byte[] data = new byte[0]; File file = new File(filePath); if (file.exists()) { FileInputStream in = new FileInputStream(file); ByteArrayOutputStream out = new ByteArrayOutputStream(2048); byte[] cache = new byte[CACHE_SIZE]; int nRead = 0; while ((nRead = in.read(cache)) != -1) { out.write(cache, 0, nRead);//from w ww . j a va2 s. c o m out.flush(); } out.close(); in.close(); data = out.toByteArray(); } return data; }
From source file:com.recomdata.transmart.data.export.util.ZipUtil.java
static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception { File folder = new File(srcFile); if (folder.isDirectory()) { addFolderToZip(path, srcFile, zip); } else {/*from w w w . j av a 2 s.com*/ byte[] buf = new byte[BUFFER_SIZE]; int len; FileInputStream in = new FileInputStream(srcFile); zip.putNextEntry(new ZipEntry(path + "/" + folder.getName())); while ((len = in.read(buf)) > 0) { zip.write(buf, 0, len); } } }
From source file:Main.java
public static void CopyFile(String source, String destination) throws IOException { FileInputStream fin = new FileInputStream(source); FileOutputStream fout = new FileOutputStream(destination); byte[] b = new byte[1024 * 1024]; int noOfBytes = 0; while ((noOfBytes = fin.read(b)) != -1) { fout.write(b, 0, noOfBytes);//from w w w . ja v a 2s . c om } fin.close(); fout.close(); }
From source file:Main.java
private static void movefile(String srcPath, String dstPath) throws IOException { File srcFile = new File(srcPath); File dstFile = new File(dstPath); if (srcFile.exists()) { FileInputStream inputStream = new FileInputStream(srcFile); FileOutputStream outputStream = new FileOutputStream(dstFile); byte[] bytes = new byte[1024]; int reads = 0; while ((reads = inputStream.read(bytes)) != -1) { outputStream.write(bytes, 0, reads); }//from w w w.j a va 2s. com srcFile.delete(); } }