可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I see that Java 8 has significantly cleaned up reading the contents of a file into a String:
String contents = new String(Files.readAllBytes(Paths.get(new URI(someUrl))));
I am wondering if there is something similar (cleaner/less code/more concise) for copying directories recursively. In Java 7 land, it's still something like:
public void copyFolder(File src, File dest) throws IOException{ if(src.isDirectory()){ if(!dest.exists()){ dest.mkdir(); } String files[] = src.list(); for (String file : files) { File srcFile = new File(src, file); File destFile = new File(dest, file); copyFolder(srcFile,destFile); } } else { InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dest); byte[] buffer = new byte[1024]; int length; while ((length = in.read(buffer)) > 0){ out.write(buffer, 0, length); } in.close(); out.close(); } }
Any improvements here in Java 8?
回答1:
How about the following code
public void copyFolder(File src, File dest) throws IOException{ try (Stream<Path> stream = Files.walk(src.toPath())) { stream.forEach(sourcePath -> { try { Files.copy( /*Source Path*/ sourcePath, /*Destination Path */ src.toPath().resolve(dest.toPath().relativize(sourcePath))); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }); } }
回答2:
This version uses Files.walk and Path parameters as Java 8 suggests.
public static void copyFolder( Path src, Path dest ) { try { Files.walk( src ) .forEach( s -> { try { Path d = dest.resolve( src.relativize(s) ); if( Files.isDirectory( s ) ) { if( !Files.exists( d ) ) Files.createDirectory( d ); return; } Files.copy( s, d );// use flag to override existing }catch( Exception e ) { e.printStackTrace(); } }); }catch( Exception ex ) { ex.printStackTrace(); } }
回答3:
In this way the code looks a bit simpler
public void copyFolder(Path src, Path dest) throws IOException { Files.walk(src) .forEach(source -> copy(source, dest.resolve(src.relativize(source)))); } private void copy(Path source, Path dest) { try { Files.copy(source, dest, REPLACE_EXISTING); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } }
回答4:
my version:
static private void copyFolder(File src, File dest) { // checks if(src==null || dest==null) return; if(!src.isDirectory()) return; if(dest.exists()){ if(!dest.isDirectory()){ //System.out.println("destination not a folder " + dest); return; } } else { dest.mkdir(); } if(src.listFiles()==null || src.listFiles().length==0) return; for(File file: src.listFiles()){ File fileDest = new File(dest, file.getName()); //System.out.println(fileDest.getAbsolutePath()); if(file.isDirectory()){ copyFolder(file, fileDest); }else{ if(fileDest.exists()) continue; try { Files.copy(file.toPath(), fileDest.toPath()); } catch (IOException e) { //e.printStackTrace(); } } } }
回答5:
and one more version:
static void copyFolder(File src, File dest){ // checks if(src==null || dest==null) return; if(!src.isDirectory()) return; if(dest.exists()){ if(!dest.isDirectory()){ //System.out.println("destination not a folder " + dest); return; } } else { dest.mkdir(); } if(src.listFiles()==null || src.listFiles().length==0) return; String strAbsPathSrc = src.getAbsolutePath(); String strAbsPathDest = dest.getAbsolutePath(); try { Files.walkFileTree(src.toPath(), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { File dstFile = new File(strAbsPathDest + file.toAbsolutePath().toString().substring(strAbsPathSrc.length())); if(dstFile.exists()) return FileVisitResult.CONTINUE; if(!dstFile.getParentFile().exists()) dstFile.getParentFile().mkdirs(); //System.out.println(file + " " + dstFile.getAbsolutePath()); Files.copy(file, dstFile.toPath()); return FileVisitResult.CONTINUE; } }); } catch (IOException e) { //e.printStackTrace(); return; } return; }
its code use java8 Files.walkFileTree function.