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(); } }
FileInputStream inputStream = new FileInputStream("foo.txt"); try { String everything = IOUtils.toString(inputStream); } finally { inputStream.close(); }
/** * Returns a buffered reader that reads from a file using the given character set. * * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link * java.nio.file.Files#newBufferedReader(java.nio.file.Path, Charset)}. * * @param file the file to read from * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for * helpful predefined constants * @return the buffered reader */ public static BufferedReader newReader(File file, Charset charset) throws FileNotFoundException { checkNotNull(file); checkNotNull(charset); return new BufferedReader(new InputStreamReader(new FileInputStream(file), charset)); }
try ( InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dest)) { // code }
String line; try ( InputStream fis = new FileInputStream("the_file_name"); InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8")); BufferedReader br = new BufferedReader(isr); ) { while ((line = br.readLine()) != null) { // Deal with the line } }
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()); } }
public static void main(String[] args) throws Exception { File file = new File(args[0]); InputStream in = new BufferedInputStream(new FileInputStream(file)); byte[] b = new byte[(int)file.length()]; in.read(b); System.out.println(HexDump.dump(b, 0, 0)); in.close(); } }
private InputStream getFileAsStream(String filePath) throws FileNotFoundException { InputStream inStream = null; File file = new File(filePath); if (file.exists()) { inStream = new FileInputStream(file); } else { inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath); } return inStream; } }
FileInputStream fis = new FileInputStream(new File("foo")); String md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis); fis.close()
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();
/** * Decompress gzip archive. */ public static File ungzip(File file) throws IOException { String outFileName = FileNameUtil.removeExtension(file.getAbsolutePath()); File out = new File(outFileName); out.createNewFile(); FileOutputStream fos = new FileOutputStream(out); GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(file)); try { StreamUtil.copy(gzis, fos); } finally { StreamUtil.close(fos); StreamUtil.close(gzis); } return out; }
@Override public byte[] getBytes(int x, int y, int zoom, String dirWithTiles) throws IOException { File f = new File(dirWithTiles, calculateTileId(x, y, zoom)); if (!f.exists()) return null; ByteArrayOutputStream bous = new ByteArrayOutputStream(); FileInputStream fis = new FileInputStream(f); Algorithms.streamCopy(fis, bous); fis.close(); bous.close(); return bous.toByteArray(); }
/** * Parse a hosts file. * * @param file the file to be parsed * @param charsets the {@link Charset}s to try as file encodings when parsing. * @return a {@link HostsFileEntries} * @throws IOException file could not be read */ public static HostsFileEntries parse(File file, Charset... charsets) throws IOException { checkNotNull(file, "file"); checkNotNull(charsets, "charsets"); if (file.exists() && file.isFile()) { for (Charset charset: charsets) { HostsFileEntries entries = parse(new BufferedReader(new InputStreamReader( new FileInputStream(file), charset))); if (entries != HostsFileEntries.EMPTY) { return entries; } } } return HostsFileEntries.EMPTY; }
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); } } }
FileInputStream fis = new FileInputStream("awesomefile.csv"); InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); CSVReader reader = new CSVReader(isr); for (String[] row; (row = reader.readNext()) != null;) { System.out.println(Arrays.toString(row)); } reader.close(); isr.close(); fis.close();
private static YamlOrchestrationMasterSlaveRuleConfiguration unmarshal(final File yamlFile) throws IOException { try ( FileInputStream fileInputStream = new FileInputStream(yamlFile); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8") ) { return new Yaml(new Constructor(YamlOrchestrationMasterSlaveRuleConfiguration.class)).loadAs(inputStreamReader, YamlOrchestrationMasterSlaveRuleConfiguration.class); } }
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() ); } }
public TraditionalSimplifiedCharacterMap(String path) { // TODO: gzipped maps might be faster try { FileInputStream fis = new FileInputStream(path); InputStreamReader isr = new InputStreamReader(fis, "utf-8"); BufferedReader br = new BufferedReader(isr); init(br); br.close(); isr.close(); fis.close(); } catch (IOException e) { throw new RuntimeIOException(e); } }
private void loadImageFromStorage(String path) { try { File f=new File(path, "profile.jpg"); Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f)); ImageView img=(ImageView)findViewById(R.id.imgPicker); img.setImageBitmap(b); } catch (FileNotFoundException e) { e.printStackTrace(); } }