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(); } }
public static String readTxt(File file, String charsetName) throws IOException { FileInputStream is = new FileInputStream(file); byte[] targetArray = new byte[is.available()]; int len; int off = 0; while ((len = is.read(targetArray, off, targetArray.length - off)) != -1 && off < targetArray.length) { off += len; } is.close(); return new String(targetArray, charsetName); }
File file = new File("a.txt"); FileInputStream fis = new FileInputStream(file); byte[] data = new byte[(int) file.length()]; fis.read(data); fis.close(); String str = new String(data, "UTF-8");
public static byte[] readBytes(File file) throws IOException { byte[] bytes = new byte[(int) file.length()]; try (FileInputStream fileInputStream = new FileInputStream(file)) { fileInputStream.read(bytes); } return bytes; }
public static void serializeSSLFile(String readFile, Map<String, String> credentials) { try (FileInputStream in = new FileInputStream(readFile)) { LOG.debug("serializing ssl file: {}", readFile); ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int length; while ((length = in.read(buffer)) != -1) { result.write(buffer, 0, length); } String resultStr = DatatypeConverter.printBase64Binary(result.toByteArray()); File f = new File(readFile); LOG.debug("ssl read files is name: {}", f.getName()); credentials.put(f.getName(), resultStr); } catch (Exception e) { throw new RuntimeException(e); } }
final FileInputStream in; if(test) in = new FileInputStream("foo.txt"); else System.out.println("test failed"); in.read(); // Compiler error because variable 'in' might be unassigned
public static void compressGZIP(File input, File output) throws IOException { try (GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(output))) { try (FileInputStream in = new FileInputStream(input)) { byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } } } }
File newSoundFile = new File("/sdcard/media/ringtone", "myringtone.oog"); Uri mUri = Uri.parse("android.resource://com.your.package/R.raw.your_resource_id"); ContentResolver mCr = app.getContentResolver(); AssetFileDescriptor soundFile; try { soundFile= mCr.openAssetFileDescriptor(mUri, "r"); } catch (FileNotFoundException e) { soundFile=null; } try { byte[] readData = new byte[1024]; FileInputStream fis = soundFile.createInputStream(); FileOutputStream fos = new FileOutputStream(newSoundFile); int i = fis.read(readData); while (i != -1) { fos.write(readData, 0, i); i = fis.read(readData); } fos.close(); } catch (IOException io) { }
@Test public void openFileInput_shouldReturnAFileInputStream() throws Exception { String fileContents = "blah"; File file = new File(context.getFilesDir(), "__test__"); try (Writer fileWriter = Files.newBufferedWriter(file.toPath(), UTF_8)) { fileWriter.write(fileContents); } try (FileInputStream fileInputStream = context.openFileInput("__test__")) { byte[] bytes = new byte[fileContents.length()]; fileInputStream.read(bytes); assertThat(bytes).isEqualTo(fileContents.getBytes(UTF_8)); } }
public static PublicKey getPublicKeyByPublicKeyFile(String publicKeyFile) { if (publicKeyFile == null || publicKeyFile.length() == 0) { return ConfigTools.getPublicKey(null); } FileInputStream in = null; try { in = new FileInputStream(publicKeyFile); ByteArrayOutputStream out = new ByteArrayOutputStream(); int len = 0; byte[] b = new byte[512 / 8]; while ((len = in.read(b)) != -1) { out.write(b, 0, len); } byte[] publicKeyBytes = out.toByteArray(); X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKeyBytes); KeyFactory factory = KeyFactory.getInstance("RSA", "SunRsaSign"); return factory.generatePublic(spec); } catch (Exception e) { throw new IllegalArgumentException("Failed to get public key", e); } finally { JdbcUtils.close(in); } }
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath(); String fileName = "myFile.txt"; // Not sure if the / is on the path or not File f = new File(baseDir + File.Separator + fileName); FileInputStream fiStream = new FileInputStream(f); byte[] bytes; // You might not get the whole file, lookup File I/O examples for Java fiStream.read(bytes); fiStream.close();
public static void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception { File folder = new File(srcFile); if (folder.isDirectory()) { addFolderToZip(path, srcFile, zip); } else { byte[] buf = new byte[1024]; 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); } } }
public void test(String f) { try { FileInputStream in = new FileInputStream(f); int b = in.read(); System.out.println(b); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (bad.contains(f)) System.out.println("oops"); } }
private CoordinatorRepositoryAdapter readRecover(final File file) { try { try (FileInputStream fis = new FileInputStream(file)) { byte[] content = new byte[(int) file.length()]; fis.read(content); return objectSerializer.deSerialize(content, CoordinatorRepositoryAdapter.class); } } catch (Exception e) { e.printStackTrace(); return null; } }
public static void compressGZIP(File input, File output) throws IOException { try (GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(output))) { try (FileInputStream in = new FileInputStream(input)) { byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } } } }
int length = (int) file.length(); byte[] bytes = new byte[length]; FileInputStream in = new FileInputStream(file); try { in.read(bytes); } finally { in.close(); } String contents = new String(bytes);
private static String readProcessName() throws IOException { byte[] cmdlineBuffer = new byte[CMDLINE_BUFFER_SIZE]; // Avoid using a Reader to not pick up a forced 16K buffer. Silly java.io... FileInputStream stream = new FileInputStream("/proc/self/cmdline"); boolean success = false; try { int n = stream.read(cmdlineBuffer); success = true; int endIndex = indexOf(cmdlineBuffer, 0, n, (byte)0 /* needle */); return new String(cmdlineBuffer, 0, endIndex > 0 ? endIndex : n); } finally { Util.close(stream, !success); } }
/** * Read a file named 'doc' and store its' bytes for later processing. * @param doc * @throws IOException */ protected void getInFromFile(String doc) throws IOException{ File documentOnDisk = new File(doc); long length = documentOnDisk.length(); document = new byte[(int)length]; FileInputStream inStream = new FileInputStream(documentOnDisk); inStream.read(document); }