canonical example by Tabnine
public void copyFile(File srcFile, File dstFile) throws IOException { try (FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(dstFile)) { int len; byte[] buffer = new byte[1024]; while ((len = fis.read(buffer)) > 0) { fos.write(buffer, 0, len); } } }
canonical example by Tabnine
public void zipFile(File srcFile, File zipFile) throws IOException { try (FileInputStream fis = new FileInputStream(srcFile); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) { zos.putNextEntry(new ZipEntry(srcFile.getName())); int len; byte[] buffer = new byte[1024]; while ((len = fis.read(buffer)) > 0) { zos.write(buffer, 0, len); } zos.closeEntry(); } }
// create a File object for the parent directory File wallpaperDirectory = new File("/sdcard/Wallpaper/"); // have the object build the directory structure, if needed. wallpaperDirectory.mkdirs(); // create a File object for the output file File outputFile = new File(wallpaperDirectory, filename); // now attach the OutputStream to the file object, instead of a String representation FileOutputStream fos = new FileOutputStream(outputFile);
FileOutputStream out = null; try { out = new FileOutputStream(filename); bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance // PNG is a lossless format, the compression factor (100) is ignored } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } }
try ( InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dest)) { // code }
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); _bitmapScaled.compress(Bitmap.CompressFormat.JPEG, 40, bytes); //you can create a new file name "test.jpg" in sdcard folder. File f = new File(Environment.getExternalStorageDirectory() + File.separator + "test.jpg"); f.createNewFile(); //write the bytes in file FileOutputStream fo = new FileOutputStream(f); fo.write(bytes.toByteArray()); // remember close de FileOutput fo.close();
public void chop() { File targetFile = new File(txnLogFile.getParentFile(), txnLogFile.getName() + ".chopped" + zxid); try ( InputStream is = new BufferedInputStream(new FileInputStream(txnLogFile)); OutputStream os = new BufferedOutputStream(new FileOutputStream(targetFile)) ) { if (!LogChopper.chop(is, os, zxid)) { throw new TxnLogToolkitException(ExitCode.INVALID_INVOCATION.getValue(), "Failed to chop %s", txnLogFile.getName()); } } catch (Exception e) { System.out.println("Got exception: " + e.getMessage()); } }
private File setupFile(String testDir, String fileName, String content) throws Exception { File file = new File(testDir, fileName); if (file.exists()) { file.delete(); } BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); out.write(content); out.close(); return file; }
private String extractLibrary (String sharedLibName) { String srcCrc = crc(JniGenSharedLibraryLoader.class.getResourceAsStream("/" + sharedLibName)); File nativesDir = new File(System.getProperty("java.io.tmpdir") + "/jnigen/" + srcCrc); File nativeFile = new File(nativesDir, sharedLibName); if (nativeFile.exists()) { try { extractedCrc = crc(new FileInputStream(nativeFile)); } catch (FileNotFoundException ignored) { input = getFromJar(nativesJar, sharedLibName); if (input == null) return null; nativeFile.getParentFile().mkdirs(); FileOutputStream output = new FileOutputStream(nativeFile); byte[] buffer = new byte[4096]; while (true) { int length = input.read(buffer); if (length == -1) break; output.write(buffer, 0, length); output.close(); } catch (IOException ex) { ex.printStackTrace(); return nativeFile.exists() ? nativeFile.getAbsolutePath() : null;
public static void copyFileUsingStream(File source, File dest) throws IOException { FileInputStream is = null; FileOutputStream os = null; File parent = dest.getParentFile(); if (parent != null && (!parent.exists())) { parent.mkdirs(); } try { is = new FileInputStream(source); os = new FileOutputStream(dest, false); byte[] buffer = new byte[TypedValue.BUFFER_SIZE]; int length; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } } finally { StreamUtil.closeQuietly(os); StreamUtil.closeQuietly(is); } }
private OutputStreamWriter getFileWriter() throws FileNotFoundException { return new OutputStreamWriter( new FileOutputStream(new File(baseDir, filePattern.print(currentDay)), true), StandardCharsets.UTF_8 ); }
public static void copyFile(InputStream from, File to) throws IOException { if (! to.getParentFile().exists()) { to.getParentFile().mkdirs(); } try (OutputStream os = new FileOutputStream(to)) { byte[] buffer = new byte[65536]; int count = from.read(buffer); while (count > 0) { os.write(buffer, 0, count); count = from.read(buffer); } } }
FileOutputStream fos = new FileOutputStream("pathname"); fos.write(myByteArray); fos.close();
public static void copyFile( File from, File to ) throws IOException { if ( !to.exists() ) { to.createNewFile(); } try ( FileChannel in = new FileInputStream( from ).getChannel(); FileChannel out = new FileOutputStream( to ).getChannel() ) { out.transferFrom( in, 0, in.size() ); } }
protected static void outChars(File dest, char[] data, String encoding, boolean append) throws IOException { if (dest.exists()) { if (!dest.isFile()) { throw new IOException(MSG_NOT_A_FILE + dest); } } Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest, append), encoding)); try { out.write(data); } finally { StreamUtil.close(out); } }
public static ElasticsearchStatus write(String path, int pid) { File f = new File(path); FileOutputStream fos = null; try { fos = new FileOutputStream(path); fos.write(String.format("%d", pid).getBytes()); } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(fos); } return new ElasticsearchStatus(f, pid); }
private Revision createNewFileAndCheckIn(File directory) { try { new FileOutputStream(new File(directory, "test.txt")).close(); addremove(directory); commit("created test.txt", directory); } catch (IOException e) { e.printStackTrace(); } return latestRevisionOf(); }
protected static void touchFile(File file) throws IOException { if (!file.exists()) { new FileOutputStream(file).close(); } if (!file.setLastModified(System.currentTimeMillis())) { throw new IOException("Could not touch the file."); } }
private void addHeaderToManifest(String header, String value) throws IOException { FileInputStream manifestInputStream = new FileInputStream(manifestFile); Manifest manifest = new Manifest(manifestInputStream); Attributes entries = manifest.getMainAttributes(); entries.put(new Attributes.Name(header), value); FileOutputStream manifestOutputStream = new FileOutputStream(manifestFile, false); manifest.write(manifestOutputStream); manifestOutputStream.close(); manifestInputStream.close(); }
public File createSegmentDescriptorFile(final ObjectMapper jsonMapper, final DataSegment segment) throws IOException { File descriptorFile = File.createTempFile("descriptor", ".json"); try (FileOutputStream stream = new FileOutputStream(descriptorFile)) { stream.write(jsonMapper.writeValueAsBytes(segment)); } return descriptorFile; }