Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- public class FileOperations
- {
- public static void deleteDirectory(File directory)
- {
- File[] children = directory.listFiles();
- for (int i = 0; i < children.length; i++)
- {
- if (children[i].isFile())
- {
- children[i].delete();
- } else
- {
- deleteDirectory(children[i]);
- children[i].delete();
- }
- }
- directory.delete();
- }
- public static void copyDirectory(File sourceLocation, File targetLocation)
- throws IOException
- {
- if (sourceLocation.isDirectory())
- {
- if (!targetLocation.exists())
- {
- targetLocation.mkdir();
- }
- File[] children = sourceLocation.listFiles();
- for (int i = 0; i < children.length; i++)
- {
- copyDirectory(children[i],
- new File(targetLocation, children[i].getName()));
- }
- } else
- {
- copyFile(sourceLocation, targetLocation);
- }
- }
- private static void copyFile(File sourceLocation, File targetLocation)
- throws IOException
- {
- InputStream inputStream = new FileInputStream(sourceLocation);
- OutputStream outputStream = new FileOutputStream(targetLocation);
- byte[] buffer = new byte[1024];
- int length;
- while ((length = inputStream.read(buffer)) > 0)
- {
- outputStream.write(buffer, 0, length);
- }
- inputStream.close();
- outputStream.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement