Skip to content

Instantly share code, notes, and snippets.

@SoltauFintel
Last active January 1, 2024 19:22
Show Gist options
  • Save SoltauFintel/b38950e4118c0f3c47b111368ddea34a to your computer and use it in GitHub Desktop.
Save SoltauFintel/b38950e4118c0f3c47b111368ddea34a to your computer and use it in GitHub Desktop.
Dateioperationen
  • alle Dateien in einem Verzeichnisbaum durchlaufen
  • Textdatei in String laden
  • Textdatei zeilenweise laden
  • Rename
  • Ordner löschen

alle Dateien in einem Verzeichnisbaum durchlaufen

Files.walkFileTree(Paths.get("../hello-web"), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
	if (file.toString().endsWith(".java")) {
		System.out.println(file);
	}
	return super.visitFile(file, attrs);
}
});

Textdatei in String laden

String content = new String(Files.readAllBytes(Paths.get("dateiname")));

Textdatei zeilenweise laden

//Path path = new File("dateiname").toPath();
Path path = Paths.get("dateiname");
try (Stream<String> lines = Files.lines(path)) {
	lines.forEach(it -> System.out.println("=> " + it));
}

Rename

Wenn man den Dateinamen ändern möchte, muss man beim Target auch den Pfad angeben. Andernfalls wird die Datei verschoben.

Ordner löschen

Ordner muss nicht leer sein und kann Unterordner haben

FileUtils.deleteDirectory(new File("work"));

compile 'commons-io:commons-io:2.6'

Thread start oder run?

new Thread(() -> { ... }).start();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment