public static void main(String[] args) throws IOException { // open file but do not close it. Its existance will be checked by // the calling script. Paths.get(args[0]).newByteChannel(READ, WRITE, DELETE_ON_CLOSE); // check temporary file has been deleted after closing it Path file = File.createTempFile("blah", "tmp").toPath(); file.newByteChannel(READ, WRITE, DELETE_ON_CLOSE).close(); if (file.exists()) throw new RuntimeException("Temporary file was not deleted"); Path dir = TestUtil.createTemporaryDirectory(); try { // check that DELETE_ON_CLOSE fails when file is a sym link if (TestUtil.supportsLinks(dir)) { file = dir.resolve("foo").createFile(); Path link = dir.resolve("link").createSymbolicLink(file); try { link.newByteChannel(READ, WRITE, DELETE_ON_CLOSE); throw new RuntimeException("IOException expected"); } catch (IOException ignore) { } } // check that DELETE_ON_CLOSE works with files created via open // directories DirectoryStream stream = dir.newDirectoryStream(); try { if (stream instanceof SecureDirectoryStream) { SecureDirectoryStream secure = (SecureDirectoryStream) stream; file = Paths.get("foo"); Set<OpenOption> opts = new HashSet<OpenOption>(); opts.add(WRITE); opts.add(DELETE_ON_CLOSE); secure.newByteChannel(file, opts).close(); if (dir.resolve(file).exists()) throw new RuntimeException("File not deleted"); } } finally { stream.close(); } } finally { TestUtil.removeAll(dir); } }
private int getNumberOfItems(Path quellOrdner) { int retValue = 0; try { DirectoryStream<Path> qstream = Files.newDirectoryStream(quellOrdner); for (Path qfile : qstream) { if (Files.isDirectory(qfile)) { getNumberOfItems(Paths.get(quellOrdner.toString() + "/" + qfile.getFileName())); } i++; } qstream.close(); } catch (IOException e) { e.printStackTrace(); } retValue = i; return retValue; }
public void cnpStart(Path quellOrdner, Path zielOrdner) { try { DirectoryStream<Path> qstream = Files.newDirectoryStream(quellOrdner); for (Path qfile : qstream) { Path target = Paths.get(zielOrdner.toString() + "/" + qfile.getFileName()); if (abbruch) break; if (Files.isDirectory(qfile) && !Files.exists(target)) { Files.createDirectory(target); textArea.append("Verzeichnis: " + qfile + " wurde erstellt" + System.lineSeparator()); cnpStart( Paths.get(quellOrdner.toString() + "/" + qfile.getFileName()), Paths.get(zielOrdner.toString() + "/" + qfile.getFileName())); } else if (Files.isDirectory(qfile) && Files.exists(target)) { textArea.append("Wechsle in Verzeichnis: " + qfile + System.lineSeparator()); cnpStart( Paths.get(quellOrdner.toString() + "/" + qfile.getFileName()), Paths.get(zielOrdner.toString() + "/" + qfile.getFileName())); } // Wenn die Datei noch nicht existiert else if (!Files.exists(target)) { textArea.append( "Datei " + target.toString() + " wurde erstellt" + System.lineSeparator()); Files.copy(qfile, target, StandardCopyOption.REPLACE_EXISTING); } // Wenn Datei im Zielverzeichnis schon existiert else if (Files.exists(target)) { if (cAUeSchr) { textArea.append( "Datei " + target.toString() + " wird absolut überschrieben" + System.lineSeparator()); Files.copy(qfile, target, StandardCopyOption.REPLACE_EXISTING); } else if (cUeSchr) { if (checkAlter( Paths.get(quellOrdner.toString() + "/" + qfile.getFileName()), Paths.get(zielOrdner.toString() + "/" + qfile.getFileName()))) { textArea.append( target.toString() + " wird mit neuer Datei überschrieben" + System.lineSeparator()); Files.copy(qfile, target, StandardCopyOption.REPLACE_EXISTING); } else { textArea.append( target.toString() + " alte Datei bleibt bestehen" + System.lineSeparator()); } } else textArea.append( target.toString() + " alte Datei bleibt bestehen" + System.lineSeparator()); } pbCounter++; progressBar.setValue(pbCounter); } qstream.close(); } catch (IOException e) { e.printStackTrace(); } }