Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class FileManager {
- public static void main(String[] args) {
- String from = "E:\\JAVA\\Luxoft Traning\\JVA-008\\Path2";
- String to = "E:\\JAVA\\Luxoft Traning\\JVA-008\\Path3";
- try {
- copy(from, to);
- } catch (IOException | NullPointerException e) {
- e.printStackTrace();
- }
- }
- public static void copy(String from, String to) throws IOException, NullPointerException {
- File source = new File(from);
- File destination = new File(to);
- InputStream inputStream;
- OutputStream outputStream;
- if (source.isFile()) {
- inputStream = new FileInputStream(source);
- outputStream = new FileOutputStream(destination);
- byte[] buffer = new byte[1024];
- int bufferSize;
- while ((bufferSize = inputStream.read(buffer)) > 0) {
- outputStream.write(buffer, 0, bufferSize);
- }
- inputStream.close();
- outputStream.close();
- } else {
- if (!destination.exists()) {
- destination.mkdir();
- }
- String files[] = source.list();
- for (String file : files) {
- File sourceFile = new File(source, file);
- File destinationFile = new File(destination, file);
- copy(sourceFile.getCanonicalPath(), destinationFile.getCanonicalPath());
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment