/** * Recursively copy a file or directory to a directory. */ public static void copy( File fileOrDirectory, File toDir ) { copy( fileOrDirectory, toDir, null ); }
/** * Copies the content of a reader to an output stream. * * @param reader the reader to read * @param out the output stream to write * * @throws IOException if an I/O error occurs */ public static void copy( Reader reader, OutputStream out ) throws IOException { copy( reader, getOutputStreamWriter( out ) ); out.flush(); }
/** * Returns the content of the specified input stream. The stream will be closed after calling this method. * * @param in the input stream to read * * @return the content of the input stream * * @throws IOException if an I/O error occurs */ public static byte[] getContent( InputStream in ) throws IOException { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); copy( in, baos ); return baos.toByteArray(); } finally { in.close(); } }
/** * Returns the content of the specified reader. The reader will be closed after calling this method. * * @param in the reader to read * * @return the content of the reader * * @throws IOException if an I/O error occurs */ public static String getContent( Reader in ) throws IOException { try { StringWriter sw = new StringWriter(); copy( in, sw ); return sw.toString(); } finally { in.close(); } }
/** * Copies the content of an input stream to a writer. * * @param in the input stream to read * @param writer the writer to write * * @throws IOException if an I/O error occurs */ public static void copy( InputStream in, Writer writer ) throws IOException { copy( getInputStreamReader( in ), writer ); writer.flush(); }
public static void copy( File fileOrDirectory, File toDir, Predicate<File> filter ) { if( filter == null || filter.test( fileOrDirectory ) ) { File copy = new File( toDir, fileOrDirectory.getName() ); if( fileOrDirectory.isDirectory() ) { //noinspection ResultOfMethodCallIgnored copy.mkdir(); for( File child : fileOrDirectory.listFiles() ) { copy( child, copy, filter ); } } else { //noinspection ResultOfMethodCallIgnored try (InputStream is = new FileInputStream( fileOrDirectory ); OutputStream os = new FileOutputStream( copy )) { StreamUtil.copy( is, os ); } catch( Exception e ) { throw new RuntimeException( e ); } } } }