Answers for "how to copy all files from one directory to another in java"

2

java copy file from one directory to another efficiently

private static void copyFileUsingStream(File source, File dest) throws IOException {
    InputStream is = null;
    OutputStream os = null;
    try {
        is = new FileInputStream(source);
        os = new FileOutputStream(dest);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = is.read(buffer)) > 0) {
            os.write(buffer, 0, length);
        }
    } finally {
        is.close();
        os.close();
    }
}
Posted by: Guest on March-04-2020
2

how to copy all files and subdirectories in directory in java

public static void storeFile(String path, File file) {
		try {
			File newFile = new File(path);
			newFile.createNewFile();
			Files.copy(file.toPath(), newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void storeDirectory(String path, File file) {
		new File(path).mkdir();
		
		for (File files : file.listFiles()) {
			if (files.isDirectory())
				storeDirectory(path + "/" + files.getName(), files);
			else
				storeFile(path + "/" + files.getName(), files);
		}
	}
Posted by: Guest on June-16-2020

Code answers related to "how to copy all files from one directory to another in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language