List of usage examples for java.io FileOutputStream flush
public void flush() throws IOException
From source file:ec.edu.chyc.manejopersonal.managebean.util.BeansUtils.java
/*** * Dado el archivo de fileUpload de primefaces (en el evento fileUpload), sube el archivo a un destino * @param origen Variable tipo UploadedFile obtenida del evento fileUpload del componente de primefaces, ah se obtendr el archivo * @param destino Archivo donde se guardar el archivo * @throws IOException Excepcin ocurrida al crear, leer o escribir el archivo *//*from w w w .j ava2s.c o m*/ public static void subirArchivoPrimefaces(UploadedFile origen, File destino) throws IOException { destino.createNewFile(); FileOutputStream fileOutputStream = null; InputStream inputStream = null; try { fileOutputStream = new FileOutputStream(destino); byte[] buffer = new byte[ServerUtils.BUFFER_SIZE]; int bulk; inputStream = origen.getInputstream(); while (true) { bulk = inputStream.read(buffer); if (bulk < 0) { break; } fileOutputStream.write(buffer, 0, bulk); fileOutputStream.flush(); } fileOutputStream.close(); inputStream.close(); } finally { if (fileOutputStream != null) { fileOutputStream.close(); } if (inputStream != null) { inputStream.close(); } } }
From source file:gov.nih.nci.ncicb.tcga.dcc.common.util.FileUtil.java
/** * Expands a tar or tar.gz archive into the given directory * * @param expandDir the target directory * @param tarOrTarGzFilename the name of the archive to expand (must be tar or tar.gz) * @throws IOException//from w w w .java2 s . c om */ public static void explodeTarOrTarGz(final File expandDir, final String tarOrTarGzFilename) throws IOException { final File file = new File(tarOrTarGzFilename); final FileInputStream fStream = new FileInputStream(file); String filenameWithoutExtension; if (tarOrTarGzFilename.endsWith(ConstantValues.COMPRESSED_ARCHIVE_EXTENSION)) { filenameWithoutExtension = getFilenameWithoutExtension(tarOrTarGzFilename, ConstantValues.COMPRESSED_ARCHIVE_EXTENSION); } else { filenameWithoutExtension = getFilenameWithoutExtension(tarOrTarGzFilename, ConstantValues.UNCOMPRESSED_ARCHIVE_EXTENSION); } GZIPInputStream gzipStream = null; TarInputStream tin = null; TarEntry tarEntry; final StringBuffer errorMsg = new StringBuffer(); try { //noinspection IOResourceOpenedButNotSafelyClosed if (tarOrTarGzFilename.endsWith(ConstantValues.COMPRESSED_ARCHIVE_EXTENSION)) { gzipStream = new GZIPInputStream(fStream); tin = new TarInputStream(gzipStream); } else { tin = new TarInputStream(fStream); } while ((tarEntry = tin.getNextEntry()) != null) { String entryName = tarEntry.getName(); final File entryFile = new File(entryName); if (!tarEntry.isDirectory()) { if (!entryName.startsWith(filenameWithoutExtension)) { errorMsg.append( "Archive files should be contained inside a single directory with the same name as the archive.\n"); } // extract out just the filename of the entry if (entryFile.getCanonicalPath().lastIndexOf(File.separator) != -1) { entryName = entryFile.getCanonicalPath() .substring(entryFile.getCanonicalPath().lastIndexOf(File.separator)); } final File destPath = new File(expandDir, entryName); FileOutputStream fout = new FileOutputStream(destPath); try { tin.copyEntryContents(fout); } finally { fout.flush(); fout.close(); fout = null; } } else { if (!entryName.equals(filenameWithoutExtension) && !entryName.equals(filenameWithoutExtension + File.separator) && !entryName.equals(filenameWithoutExtension + "/")) { String t = filenameWithoutExtension + File.separator; errorMsg.append("Archive contains a non-standard directory '").append(entryName).append( "'. Archive files should be contained inside a single directory with the same name as the archive.\n"); } } } } finally { IOUtils.closeQuietly(tin); IOUtils.closeQuietly(gzipStream); IOUtils.closeQuietly(fStream); if (errorMsg.length() > 0) throw new IOException(errorMsg.toString()); } }
From source file:com.owncloud.android.lib.testclient.TestActivity.java
/** * Extracts file from AssetManager to cache folder. * // w w w . ja va 2s.com * @param fileName Name of the asset file to extract. * @param context Android context to access assets and file system. * @return File instance of the extracted file. */ public static File extractAsset(String fileName, Context context) throws IOException { File extractedFile = new File(context.getCacheDir() + File.separator + fileName); if (!extractedFile.exists()) { InputStream in = null; FileOutputStream out = null; in = context.getAssets().open(fileName); out = new FileOutputStream(extractedFile); byte[] buffer = new byte[BUFFER_SIZE]; int readCount; while ((readCount = in.read(buffer)) != -1) { out.write(buffer, 0, readCount); } out.flush(); out.close(); in.close(); } return extractedFile; }
From source file:de.tu_dortmund.ub.data.util.TPUUtil.java
public static String writeResultToFile(final CloseableHttpResponse httpResponse, final Properties config, final String exportDataModelID, final String fileEnding) throws IOException, TPUException { LOG.info("try to write result to file"); final String persistInFolderString = config.getProperty(TPUStatics.PERSIST_IN_FOLDER_IDENTIFIER); final boolean persistInFolder = Boolean.parseBoolean(persistInFolderString); final HttpEntity entity = httpResponse.getEntity(); final String fileName; if (persistInFolder) { final InputStream responseStream = entity.getContent(); final BufferedInputStream bis = new BufferedInputStream(responseStream, 1024); final String resultsFolder = config.getProperty(TPUStatics.RESULTS_FOLDER_IDENTIFIER); fileName = resultsFolder + File.separatorChar + EXPORT_FILE_NAME_PREFIX + exportDataModelID + DOT + fileEnding;/* ww w . j a v a 2s .c o m*/ LOG.info(String.format("start writing result to file '%s'", fileName)); final FileOutputStream outputStream = new FileOutputStream(fileName); final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream); IOUtils.copy(bis, bufferedOutputStream); bufferedOutputStream.flush(); outputStream.flush(); bis.close(); responseStream.close(); bufferedOutputStream.close(); outputStream.close(); checkResultForError(fileName); } else { fileName = "[no file name available]"; } EntityUtils.consume(entity); return fileName; }
From source file:com.cloverstudio.spikademo.couchdb.ConnectionHandler.java
/** * Get a File object from an URL/*from ww w.ja va 2 s . c om*/ * * @param url * @param path * @return */ public static void getFile(String url, File file, String userId, String token) { File mFile = file; try { //URL mUrl = new URL(url); // you can write here any link InputStream is = httpGetRequest(url, userId); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(20000); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } /* Convert the Bytes read to a String. */ FileOutputStream fos = new FileOutputStream(mFile); fos.write(baf.toByteArray()); fos.flush(); fos.close(); is.close(); } catch (Exception e) { Logger.error(TAG + "getFile", "Error retrieving file: ", e); } }
From source file:io.bci.BitcoinSTLGenerator.java
private static void saveKeys(ByteArrayOutputStream pub, ByteArrayOutputStream priv) { String pubFileName = getFilePrefix() + "pubKey"; String privFileName = getFilePrefix() + "privKey"; File pubFile = new File("images/" + pubFileName + ".png"); File privFile = new File("images/" + privFileName + ".png"); // Saving public key first try {/* ww w . ja va 2 s. c o m*/ FileOutputStream fout = new FileOutputStream(pubFile); fout.write(pub.toByteArray()); fout.flush(); fout.close(); } catch (FileNotFoundException e) { System.out.println("ERROR: Public key file creation failed: " + pubFile.toString()); e.printStackTrace(); } catch (IOException e) { System.out.println("ERROR: Public key file read/write failed: " + pubFile.toString()); e.printStackTrace(); } finally { System.out.println("Public key file created: " + pubFile.toString()); } // Then saving the private key try { FileOutputStream fout = new FileOutputStream(privFile); fout.write(priv.toByteArray()); fout.flush(); fout.close(); } catch (FileNotFoundException e) { System.out.println("ERROR: Private key file creation failed: " + privFile.toString()); e.printStackTrace(); } catch (IOException e) { System.out.println("ERROR: Private key file read/write failed: " + privFile.toString()); e.printStackTrace(); } finally { System.out.println("Private key file created: " + privFile.toString()); } // Checking for noStl flag. If set, don't create stl files if (!noStl) { drawSTL(pubFile, pubFileName); drawSTL(privFile, privFileName); } }
From source file:io.bci.BitcoinSTLGenerator.java
private static void saveKeys(ByteArrayOutputStream pub, ByteArrayOutputStream priv, int index) { String pubFileName = getFilePrefix() + "pubKey_" + index; String privFileName = getFilePrefix() + "privKey_" + index; File pubFile = new File("images/" + pubFileName + ".png"); File privFile = new File("images/" + privFileName + ".png"); // Saving public key first try {//from www . j a v a 2 s.c o m FileOutputStream fout = new FileOutputStream(pubFile); fout.write(pub.toByteArray()); fout.flush(); fout.close(); } catch (FileNotFoundException e) { System.out.println("ERROR: Public key file creation failed: " + pubFile.toString()); e.printStackTrace(); } catch (IOException e) { System.out.println("ERROR: Public key file read/write failed: " + pubFile.toString()); e.printStackTrace(); } finally { System.out.println("Public key file created: " + pubFile.toString()); } // Then saving the private key try { FileOutputStream fout = new FileOutputStream(privFile); fout.write(priv.toByteArray()); fout.flush(); fout.close(); } catch (FileNotFoundException e) { System.out.println("ERROR: Private key file creation failed: " + privFile.toString()); e.printStackTrace(); } catch (IOException e) { System.out.println("ERROR: Private key file read/write failed: " + privFile.toString()); e.printStackTrace(); } finally { System.out.println("Private key file created: " + privFile.toString()); } // Checking for noStl flag. If set, don't create stl files if (!noStl) { drawSTL(pubFile, pubFileName); drawSTL(privFile, privFileName); } }
From source file:io.trivium.Central.java
/** * creates a executable shell script from the original trivium.jar file *//* w w w . j a v a 2 s. c o m*/ public static void uuencode() { try { FileInputStream fis = new FileInputStream(new File("trivium.jar")); FileOutputStream fos = new FileOutputStream(new File("trivium.sh")); String head = "#!/bin/bash\n" + "uudecode $0\n" + "java -jar trivium.jar -Djava.system.class.loader=io.trivium.TriviumLoader -Djava.protocol.handler.pkgs=io.trivium.urlhandler\n" + "exit\n\n"; fos.write(head.getBytes()); UUEncoder uuec = new UUEncoder("trivium.jar"); uuec.encodeBuffer(fis, fos); fos.flush(); fos.close(); fis.close(); } catch (Exception e) { logger.log(Level.SEVERE, "error creating script", e); } }
From source file:org.ednovo.gooru.converter.service.ConversionServiceImpl.java
private static String saveAsHtml(String fileName, String content) { File file = new File(fileName); try {/*from www . j a v a2 s .c om*/ FileOutputStream fop = new FileOutputStream(file); if (!file.exists()) { file.createNewFile(); } byte[] contentInBytes = content.getBytes(); fop.write(contentInBytes); fop.flush(); fop.close(); return fileName; } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:og.android.tether.system.WebserviceTask.java
public static boolean downloadBluetoothModule(String downloadFileUrl, String destinationFilename) { if (android.os.Environment.getExternalStorageState() .equals(android.os.Environment.MEDIA_MOUNTED) == false) { return false; }/* w ww. j ava2 s . co m*/ File bluetoothDir = new File(BLUETOOTH_FILEPATH); if (bluetoothDir.exists() == false) { bluetoothDir.mkdirs(); } if (downloadFile(downloadFileUrl, "", destinationFilename) == true) { try { FileOutputStream out = new FileOutputStream(new File(destinationFilename.replace(".gz", ""))); FileInputStream fis = new FileInputStream(destinationFilename); GZIPInputStream gzin = new GZIPInputStream(new BufferedInputStream(fis)); int count; byte buf[] = new byte[8192]; while ((count = gzin.read(buf, 0, 8192)) != -1) { //System.out.write(x); out.write(buf, 0, count); } out.flush(); out.close(); gzin.close(); File inputFile = new File(destinationFilename); inputFile.delete(); } catch (IOException e) { return false; } return true; } else return false; }