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
public static void copyFile(File in, File out) throws IOException { FileInputStream fis = new FileInputStream(in); if (!out.exists()) { if (in.isDirectory()) { out.mkdirs();//from w w w .j ava 2 s . c o m } else { File parentDir = new File(out.getParent()); if (!parentDir.exists()) { parentDir.mkdirs(); } out.createNewFile(); } } FileOutputStream fos = new FileOutputStream(out); try { byte[] buf = new byte[8192]; int i = 0; while ((i = fis.read(buf)) != -1) fos.write(buf, 0, i); } catch (IOException e) { throw e; } finally { fis.close(); fos.close(); } }
From source file:Main.java
private static File getFromMediaUriPfd(Context context, ContentResolver resolver, Uri uri) { if (uri == null) return null; FileInputStream input = null; FileOutputStream output = null; try {// w ww.j a v a2 s . co m ParcelFileDescriptor pfd = resolver.openFileDescriptor(uri, "r"); FileDescriptor fd = pfd.getFileDescriptor(); input = new FileInputStream(fd); String tempFilename = getTempFilename(context); output = new FileOutputStream(tempFilename); int read; byte[] bytes = new byte[4096]; while ((read = input.read(bytes)) != -1) { output.write(bytes, 0, read); } return new File(tempFilename); } catch (IOException ignored) { // Nothing we can do } finally { closeSilently(input); closeSilently(output); } return null; }
From source file:DiskIO.java
/** * Copies the contents of any disk file to the specified output file. The * output file will be overridden if it exist. Function reports errors. * /*from w ww . j av a2s .com*/ * @param inputFile * a File object * @param outputFile * a File object * @throws java.io.IOException * if the function could not be completed because of an IO error */ public static void copyFile(File inputFile, File outputFile) throws java.io.IOException { FileInputStream in; FileOutputStream out; byte[] buffer = new byte[512]; int len; try { out = new FileOutputStream(outputFile); in = new FileInputStream(inputFile); while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } in.close(); out.close(); } catch (IOException e) { LOG.info("*** error during file copy " + outputFile.getAbsolutePath() + ": " + e.getMessage()); throw e; } }
From source file:net.vexelon.bgrates.Utils.java
/** * Move a file stored in the cache to the internal storage of the specified context * @param context/*from ww w.ja va2 s .c o m*/ * @param cacheFile * @param internalStorageName */ public static boolean moveCacheFile(Context context, File cacheFile, String internalStorageName) { boolean ret = false; FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(cacheFile); ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); byte[] buffer = new byte[1024]; int read = -1; while ((read = fis.read(buffer)) != -1) { baos.write(buffer, 0, read); } baos.close(); fis.close(); fos = context.openFileOutput(internalStorageName, Context.MODE_PRIVATE); baos.writeTo(fos); fos.close(); // delete cache cacheFile.delete(); ret = true; } catch (Exception e) { //Log.e(TAG, "Error saving previous rates!"); } finally { try { if (fis != null) fis.close(); } catch (IOException e) { } try { if (fos != null) fos.close(); } catch (IOException e) { } } return ret; }
From source file:de.fu_berlin.inf.dpp.netbeans.feedback.ErrorLogManager.java
/** * Convenience wrapper method to upload an error log file to the server. To * save time and storage space, the log is compressed to a zip archive with * the given zipName.// ww w .java2 s .com * * @param zipName * a name for the zip archive, e.g. with added user ID to make it * unique, zipName must be at least 3 characters long! * @throws IOException * if an I/O error occurs */ private static void uploadErrorLog(String zipName, File file, IProgressMonitor monitor) throws IOException { if (ERROR_LOG_UPLOAD_URL == null) { log.warn("error log upload url is not configured, cannot upload error log file"); return; } File archive = new File(System.getProperty("java.io.tmpdir"), zipName + ".zip"); ZipOutputStream out = null; FileInputStream in = null; byte[] buffer = new byte[8192]; try { in = new FileInputStream(file); out = new ZipOutputStream(new FileOutputStream(archive)); out.putNextEntry(new ZipEntry(file.getName())); int read; while ((read = in.read(buffer)) > 0) out.write(buffer, 0, read); out.finish(); out.close(); FileSubmitter.uploadFile(archive, ERROR_LOG_UPLOAD_URL, monitor); } finally { IOUtils.closeQuietly(out); IOUtils.closeQuietly(in); archive.delete(); } }
From source file:de.nrw.hbz.deepzoomer.fileUtil.FileUtil.java
/** * <p><em>Title: Loads File into an Base64 encoded Stream</em></p> * <p>Description: </p>/*www . j a v a2 s . com*/ * * @param origPidfFile * @return */ public static byte[] loadFileIntoStream(File origPidfFile) { FileInputStream fis = null; byte[] pdfStream = null; byte[] pdfRawStream = null; try { fis = new FileInputStream(origPidfFile); int i = (int) origPidfFile.length(); byte[] b = new byte[i]; fis.read(b); pdfRawStream = b; pdfStream = Base64.encodeBase64(pdfRawStream); } catch (IOException ioExc) { System.out.println(ioExc); log.error(ioExc); } finally { if (fis != null) { try { fis.close(); } catch (IOException ioExc) { log.error(ioExc); } } } return pdfStream; }
From source file:edu.uci.ics.asterix.event.service.AsterixEventServiceUtil.java
private static void replaceInJar(File sourceJar, String origFile, File replacementFile) throws IOException { String srcJarAbsPath = sourceJar.getAbsolutePath(); String srcJarSuffix = srcJarAbsPath.substring(srcJarAbsPath.lastIndexOf(File.separator) + 1); String srcJarName = srcJarSuffix.split(".jar")[0]; String destJarName = srcJarName + "-managix"; String destJarSuffix = destJarName + ".jar"; File destJar = new File(sourceJar.getParentFile().getAbsolutePath() + File.separator + destJarSuffix); // File destJar = new File(sourceJar.getAbsolutePath() + ".modified"); JarFile sourceJarFile = new JarFile(sourceJar); Enumeration<JarEntry> entries = sourceJarFile.entries(); JarOutputStream jos = new JarOutputStream(new FileOutputStream(destJar)); byte[] buffer = new byte[2048]; int read;/* ww w .j ava2 s . co m*/ while (entries.hasMoreElements()) { JarEntry entry = (JarEntry) entries.nextElement(); String name = entry.getName(); if (name.equals(origFile)) { continue; } InputStream jarIs = sourceJarFile.getInputStream(entry); jos.putNextEntry(entry); while ((read = jarIs.read(buffer)) != -1) { jos.write(buffer, 0, read); } jarIs.close(); } sourceJarFile.close(); JarEntry entry = new JarEntry(origFile); jos.putNextEntry(entry); FileInputStream fis = new FileInputStream(replacementFile); while ((read = fis.read(buffer)) != -1) { jos.write(buffer, 0, read); } fis.close(); jos.close(); sourceJar.delete(); destJar.renameTo(sourceJar); destJar.setExecutable(true); }
From source file:com.nridge.core.base.std.FilUtl.java
/** * Compresses the input file into a GZIP file container. * * @param anInPathFileName The file name to be compressed. * @param aGzipPathFileName The file name of the ZIP container. * * @throws IOException Related to opening the file streams and * related read/write operations.// www .j av a 2s .c o m */ public static void gzipFile(String anInPathFileName, String aGzipPathFileName) throws IOException { int byteCount; FileInputStream fileInputStream = new FileInputStream(anInPathFileName); FileOutputStream fileOutputStream = new FileOutputStream(aGzipPathFileName); GZIPOutputStream gzipOutputStream = new GZIPOutputStream(fileOutputStream); byte[] ioBuf = new byte[FILE_IO_BUFFER_SIZE]; byteCount = fileInputStream.read(ioBuf); while (byteCount > 0) { gzipOutputStream.write(ioBuf, 0, byteCount); byteCount = fileInputStream.read(ioBuf); } gzipOutputStream.close(); fileOutputStream.close(); fileInputStream.close(); }
From source file:de.ingrid.iplug.csw.dsc.tools.FileUtils.java
/** * This function will copy files or directories from one location to another. * note that the source and the destination must be mutually exclusive. This * function can not be used to copy a directory to a sub directory of itself. * The function will also have problems if the destination files already exist. * @param src -- A File object that represents the source for the copy * @param dest -- A File object that represnts the destination for the copy. * @throws IOException if unable to copy. * //from w w w . j a v a2s .c om * Source: http://www.dreamincode.net/code/snippet1443.htm */ public static void copyRecursive(File src, File dest) throws IOException { //Check to ensure that the source is valid... if (!src.exists()) { throw new IOException("copyFiles: Can not find source: " + src.getAbsolutePath() + "."); } else if (!src.canRead()) { //check to ensure we have rights to the source... throw new IOException("copyFiles: No right to source: " + src.getAbsolutePath() + "."); } //is this a directory copy? if (src.isDirectory()) { if (!dest.exists()) { //does the destination already exist? //if not we need to make it exist if possible (note this is mkdirs not mkdir) if (!dest.mkdirs()) { throw new IOException("copyFiles: Could not create direcotry: " + dest.getAbsolutePath() + "."); } } //get a listing of files... String list[] = src.list(); //copy all the files in the list. for (int i = 0; i < list.length; i++) { File dest1 = new File(dest, list[i]); File src1 = new File(src, list[i]); copyRecursive(src1, dest1); } } else { //This was not a directory, so lets just copy the file FileInputStream fin = null; FileOutputStream fout = null; byte[] buffer = new byte[4096]; //Buffer 4K at a time (you can change this). int bytesRead; try { //open the files for input and output fin = new FileInputStream(src); fout = new FileOutputStream(dest); //while bytesRead indicates a successful read, lets write... while ((bytesRead = fin.read(buffer)) >= 0) { fout.write(buffer, 0, bytesRead); } fin.close(); fout.close(); fin = null; fout = null; } catch (IOException e) { //Error copying file... IOException wrapper = new IOException("copyFiles: Unable to copy file: " + src.getAbsolutePath() + "to" + dest.getAbsolutePath() + "."); wrapper.initCause(e); wrapper.setStackTrace(e.getStackTrace()); throw wrapper; } finally { //Ensure that the files are closed (if they were open). if (fin != null) { fin.close(); } if (fout != null) { fin.close(); } } } }
From source file:com.baidu.rigel.biplatform.ma.file.serv.util.LocalFileOperationUtils.java
/** * ???// w ww . jav a 2s .c o m * * @param oldFile * * @param newFile * * @return * @throws IOException */ private static boolean copy(File oldFile, File newFile) { FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { fileInputStream = new FileInputStream(oldFile); fileOutputStream = new FileOutputStream(newFile); byte[] buf = new byte[1024]; int len = 0; // ?? while ((len = fileInputStream.read(buf)) != -1) { fileOutputStream.write(buf, 0, len); fileOutputStream.flush(); } return true; } catch (IOException e) { LOG.error(e.getMessage(), e); return false; } finally { try { if (fileInputStream != null) { fileInputStream.close(); } if (fileOutputStream != null) { fileOutputStream.close(); } } catch (IOException e) { LOG.error(e.getMessage(), e); } } }