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); } } }
FileOutputStream fos = new FileOutputStream("pathname"); fos.write(myByteArray); fos.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; }
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();
void openOutputStream_noParent(final boolean createFile) throws Exception { final File file = new File("test.txt"); assertNull(file.getParentFile()); try { if (createFile) { TestUtils.createLineBasedFile(file, new String[]{"Hello"}); } try (FileOutputStream out = FileUtils.openOutputStream(file)) { out.write(0); } assertTrue(file.exists()); } finally { if (!file.delete()) { file.deleteOnExit(); } } }
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); }
protected static void outString(File dest, String data, String encoding, boolean append) throws IOException { if (dest.exists()) { if (!dest.isFile()) { throw new IOException(MSG_NOT_A_FILE + dest); } } FileOutputStream out = null; try { out = new FileOutputStream(dest, append); out.write(data.getBytes(encoding)); } finally { StreamUtil.close(out); } }
private void doSaveUnknownFile(File inFile, File outFile) throws Exception { try (FileInputStream in = new FileInputStream(inFile); FileOutputStream out = new FileOutputStream(outFile);) { System.out.println("[SaveAll]: " + inFile.getName() + " -> " + outFile.getName()); byte data[] = new byte[1024]; int count; while ((count = in.read(data, 0, 1024)) != -1) { out.write(data, 0, count); } } }
public static void writeResourceToFile(File file, String resourceName, Class<?> clasz) throws IOException { InputStream inputStream = clasz.getResourceAsStream("/" + resourceName); if (inputStream == null) { LOG.error("Couldn't find resource on the class path: " + resourceName); return; } try { try (FileOutputStream outputStream = new FileOutputStream(file)) { int nread; byte[] buffer = new byte[4096]; while (0 < (nread = inputStream.read(buffer))) { outputStream.write(buffer, 0, nread); } } } finally { inputStream.close(); } }
InputStream ins = context.getResources().openRawResource (R.raw.FILENAME) byte[] buffer = new byte[ins.available()]; ins.read(buffer); ins.close(); FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE); fos.write(buffer); fos.close(); File file = getFileStreamPath (FILENAME); file.setExecutable(true);
//create a file to write bitmap data File f = new File(context.getCacheDir(), filename); f.createNewFile(); //Convert bitmap to byte array Bitmap bitmap = your bitmap; ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos); byte[] bitmapdata = bos.toByteArray(); //write the bytes in file FileOutputStream fos = new FileOutputStream(f); fos.write(bitmapdata); fos.flush(); fos.close();
public static HBaseStatus write(String path, String hbaseVersion) { File f = new File(path); FileOutputStream fos = null; try { fos = new FileOutputStream(path); fos.write(String.format("%s", hbaseVersion).getBytes()); } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(fos); } return new HBaseStatus(f, hbaseVersion); }
public static void writeFile(byte[] data, File out) throws IOException { FileOutputStream fos = new FileOutputStream(out); fos.write(data); fos.close(); }
protected static void outBytes(File dest, byte[] data, int off, int len, boolean append) throws IOException { if (dest.exists()) { if (!dest.isFile()) { throw new IOException(MSG_NOT_A_FILE + dest); } } FileOutputStream out = null; try { out = new FileOutputStream(dest, append); out.write(data, off, len); } finally { StreamUtil.close(out); } }
/** * Checks if writing {@code len} bytes would go over threshold, and switches to file buffering if * so. */ private void update(int len) throws IOException { if (file == null && (memory.getCount() + len > fileThreshold)) { File temp = File.createTempFile("FileBackedOutputStream", null); if (resetOnFinalize) { // Finalizers are not guaranteed to be called on system shutdown; // this is insurance. temp.deleteOnExit(); } FileOutputStream transfer = new FileOutputStream(temp); transfer.write(memory.getBuffer(), 0, memory.getCount()); transfer.flush(); // We've successfully transferred the data; switch to writing to file out = transfer; file = temp; memory = null; } } }
File f = new File(getCacheDir()+"/m1.map"); if (!f.exists()) try { InputStream is = getAssets().open("m1.map"); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); FileOutputStream fos = new FileOutputStream(f); fos.write(buffer); fos.close(); } catch (Exception e) { throw new RuntimeException(e); } mapView.setMapFile(f.getPath());
private static void generateRandomDirs(File dir, int numFiles, int numDirs, int depth) throws IOException { // generate the random files for (int i = 0; i < numFiles; i++) { File file = new File(dir, new AbstractID().toString()); try (FileOutputStream out = new FileOutputStream(file)) { out.write(1); } } if (depth > 0) { // generate the directories for (int i = 0; i < numDirs; i++) { File subdir = new File(dir, new AbstractID().toString()); assertTrue(subdir.mkdir()); generateRandomDirs(subdir, numFiles, numDirs, depth - 1); } } }
FileOutputStream stream = new FileOutputStream(path); try { stream.write(bytes); } finally { stream.close(); }