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 void copy(File src, File dst) throws IOException { InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dst); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); }
FileInputStream inputStream = new FileInputStream("foo.txt"); try { String everything = IOUtils.toString(inputStream); } finally { inputStream.close(); }
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); } } } }
/** * Creates a new instance that fetches data from the specified file. * * @param chunkSize the number of bytes to fetch on each * {@link #readChunk(ChannelHandlerContext)} call */ public ChunkedNioFile(File in, int chunkSize) throws IOException { this(new FileInputStream(in).getChannel(), chunkSize); }
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); }
/** * 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)); }
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()); } }
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 static void bsdiff(File oldFile, File newFile, File diffFile) throws IOException { InputStream oldInputStream = new BufferedInputStream(new FileInputStream(oldFile)); InputStream newInputStream = new BufferedInputStream(new FileInputStream(newFile)); OutputStream diffOutputStream = new FileOutputStream(diffFile); try { byte[] diffBytes = bsdiff(oldInputStream, (int) oldFile.length(), newInputStream, (int) newFile.length()); diffOutputStream.write(diffBytes); } finally { diffOutputStream.close(); } }
public void publicizeResources(File arscFile) throws AndrolibException { byte[] data = new byte[(int) arscFile.length()]; try(InputStream in = new FileInputStream(arscFile); OutputStream out = new FileOutputStream(arscFile)) { in.read(data); publicizeResources(data); out.write(data); } catch (IOException ex){ throw new AndrolibException(ex); } }
public SetupPreferences () { if (!file.exists()) return; InputStream in = null; try { in = new BufferedInputStream(new FileInputStream(file)); properties.loadFromXML(in); } catch (Throwable t) { t.printStackTrace(); } finally { if (in != null) try { in.close(); } catch (IOException e) { } } }
public static void cpdir(File src, File dest) throws BrutException { dest.mkdirs(); File[] files = src.listFiles(); for (int i = 0; i < files.length; i++) { File file = files[i]; File destFile = new File(dest.getPath() + File.separatorChar + file.getName()); if (file.isDirectory()) { cpdir(file, destFile); continue; } try { InputStream in = new FileInputStream(file); OutputStream out = new FileOutputStream(destFile); IOUtils.copy(in, out); in.close(); out.close(); } catch (IOException ex) { throw new BrutException("Could not copy file: " + file, ex); } } }
public void writeRequest(final OutputStream out) throws IOException { byte[] tmp = new byte[4096]; int i = 0; InputStream instream = new FileInputStream(this.file); try { while ((i = instream.read(tmp)) >= 0) { out.write(tmp, 0, i); } } finally { instream.close(); } }
try ( InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dest)) { // code }
if (extractedFile.exists()) { try { extractedCrc = crc(new FileInputStream(extractedFile)); } catch (FileNotFoundException ignored) { input = readFile(sourcePath); extractedFile.getParentFile().mkdirs(); output = new FileOutputStream(extractedFile); byte[] buffer = new byte[4096]; while (true) { int length = input.read(buffer); if (length == -1) break; output.write(buffer, 0, length);
public static int countLines(String filename) throws IOException { InputStream is = new BufferedInputStream(new FileInputStream(filename)); try { byte[] c = new byte[1024]; int count = 0; int readChars = 0; boolean empty = true; while ((readChars = is.read(c)) != -1) { empty = false; for (int i = 0; i < readChars; ++i) { if (c[i] == '\n') { ++count; } } } return (count == 0 && !empty) ? 1 : count; } finally { is.close(); } }
public static void gzip(File source, File dest, boolean deleteSource) throws IOException { byte[] buffer = new byte[2^20]; GZIPOutputStream out = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(dest), 65536)){{def.setLevel(Deflater.BEST_COMPRESSION);}}; FileInputStream in = new FileInputStream(source); int l; while ((l = in.read(buffer)) > 0) out.write(buffer, 0, l); in.close(); out.finish(); out.close(); if (deleteSource && dest.exists()) source.delete(); }
/** * Gets an input stream for the corresponding file. * * @param file The file to create the input stream from. * @return InputStream */ protected InputStream getInputStream(File file) throws IOException, FileNotFoundException { BufferedInputStream reader = new BufferedInputStream(new FileInputStream(file)); return reader; }