MathWellan

IOUtils

May 26th, 2022
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. public static void copy(File source, File target) throws IOException
  2.     {
  3.         if (source.isDirectory())
  4.         {
  5.             if (!target.exists())
  6.                 target.mkdir();
  7.  
  8.             String[] children = source.list();
  9.             for (int i = 0; i < children.length; i++)
  10.                 copy(new File(source, children[i]), new File(target, children[i]));            
  11.         }
  12.         else
  13.         {
  14.             InputStream in = new FileInputStream(source);
  15.             OutputStream out = new FileOutputStream(target);
  16.  
  17.             byte[] buf = new byte[1024];
  18.             int len;
  19.             while ((len = in.read(buf)) > 0)
  20.                 out.write(buf, 0, len);
  21.          
  22.             in.close();
  23.             out.close();
  24.         }
  25.     }
Advertisement
Add Comment
Please, Sign In to add comment