Methoden Files.walkFileTree implementation

Loooki

Mitglied
Hey,

mein Quellcode:

Java:
package de.loki.packagename;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

public class FileTool extends SimpleFileVisitor<Path> {

	private final List<File>	fileList	= new ArrayList<File>();

	public FileTool() {
	}

	public List<File> walk(String[] args) throws IOException {
		FileTool fileVisitor = new FileTool();
		for (final String arg : args.length > 0 ? args : new String[] { "." }) {
			final Path root = Paths.get(arg);
			Files.walkFileTree(root, fileVisitor);
		}
		return fileList;
	}

	@Override
	public FileVisitResult visitFile(Path pathFile, BasicFileAttributes attrs) throws IOException {
		if (!pathFile.toString().toLowerCase().endsWith("mp3")) {
			System.out.println("non mp3 File: " + pathFile.toString());
			return FileVisitResult.CONTINUE;
		}
		fileList.add(pathFile.toFile());
		System.out.println("added: " + pathFile.toString());
		return FileVisitResult.CONTINUE;
	}
}

die fileList ist leer wenn sie zurück gegeben wird wobei definitiv inhalt drinn ist denn es wird ein paar mal add aufgerufen.

Das liegt daran das die fileList nicht static ist. Doch warum ist das so? Bei static würde es sich um das gleiche Objekt handeln ohne den modifier eben nicht. Doch gilt das auch bei diesem Fall?

Files.walkFileTree ist eine statische Methode.

Nur mit dem JDK 1.7 kompilierbar.
 
Weil du eine neue Instanz von FileTool erstellst und die Liste dadrin gefüllt wird.

So sollte es funktionieren:

Java:
Files.walkFileTree(root, this);
 
Danke, oh man war ich blöd :lol:

Lag daran das es vorher auch ne statische Methode war 😀

Wirkt ja fast so als entwickle ich mich zurück^^
 

Zurück
Oben