Sprawdzenie kodu

0
class JPGFileFilter implements FileFilter {

    @Override
    public boolean accept(File pathname) {

        if (pathname.getName().endsWith(".jpg")) {
            return true;
        }
        return false;
    }

    public static File[] countImage() throws UnsupportedEncodingException {
        String path = ChooseForm.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        String decoded = URLDecoder.decode(path, "UTF-8");
        File fileOuttemp = new File(decoded);
        File cwd = fileOuttemp.getParentFile();
        File[] jpgFiles = cwd.listFiles(new JPGFileFilter());
        if (jpgFiles.length > 0) {
            return jpgFiles;
        }
        return null;
    }
} 

Metoda countImage() ma za zadanie zwracać tablice ze znalezionymi plikami. Działa ok, ale czy kod jest poprawny szczególnie ta linia: File[] jpgFiles = cwd.listFiles(new JPGFileFilter()); wywołanie konstruktora klasy w tej samej klasie klasie?

0

A nie możesz tak?

File[] jpgFiles = cwd.listFiles(this);
0

no moge chodzilo mi o to czy to jest poprawnie :)

0

EDit: nie moge wstawic tam THIS!

0
 class JPGFileFilter implements FileFilter {

    @Override
    public boolean accept(File pathname) {

        if (pathname.getName().endsWith(".jpg") || pathname.getName().endsWith(".JPG") || pathname.getName().endsWith(".jpeg")) {
            return true;
        }
        return false;
    }
    public File[] getImages() throws UnsupportedEncodingException {
        String path = ChooseForm.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        String decoded = URLDecoder.decode(path, "UTF-8");
        File fileOuttemp = new File(decoded);
        File cwd = fileOuttemp.getParentFile();
        File[] jpgFiles = cwd.listFiles(this);
        if (jpgFiles.length > 0) {
            return jpgFiles;
        }
        return null;
    }
}

Czy teraz po poprawkach jest ok? Wczesniej metoda byla static dlatego nie moglam uzyc this :)

0

Nie doceniasz ludzkiej fantazji.

        if (pathname.getName().toLowerCase().endsWith(".jpg") || pathname.getName().toLowerCase().endsWith.(".jpeg")) {
            return true;
        }
0

No skróciłeś o jedno OR :P
Dzieki

0

Tak w ogóle, to nie można tego zapisać po prostu tak:

return pathname.getName().endsWith(".jpg") || pathname.getName().endsWith(".jpeg")

?
Ale to i tak straszny kod :D

0

Twoj kod Patryk nie bedzie dzialal dla .JPG. Straszny kod ;p

0

Może coś takiego:

return Arrays.asList(".jpg", ".jpeg").contains(pathname.getName().toLowerCase());

?
Edit: nie zauważyłem, że jest jeszcze "endsWith"...

0

tak własciwie to wyrzuciłbym getImages poza filter - bo to sie ze sobą gryzie , filter to nie DAO, powinien miec jedna fukcjonalnosc jak sama nazwa wskazuje.
coś w tym stylu myśle że mogło by być.

class JPGFileFilter implements FileFilter {

	@Override
	public boolean accept(File pathname) {
		if (pathname == null)
			return false;

		if (pathname.getName().toLowerCase().endsWith(".jpg") || pathname.getName().endsWith(".jpeg"))
			return true;
		
		return false;
	}

}

public class ImagesDAO {

	public static File[] getImages(FileFilter filter) {
		
		String path = ChooseForm.class.getProtectionDomain().getCodeSource().getLocation().getPath();
		
		String decoded;
		try {
			decoded = URLDecoder.decode(path, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			System.out.println("problem with decoding: " + e);
			return null;
		}
		File fileOuttemp = new File(decoded);
		File cwd = fileOuttemp.getParentFile();
		File[] jpgFiles = cwd.listFiles(filter);
		if (jpgFiles.length > 0) {
			return jpgFiles;
		}
		return null;

	}

}

public class MainClass {
	
	public static void main(String[] args) {
		File[] files = ImagesDAO.getImages(new JPGFileFilter(); 
	}

}


0

Gdy robie jak ty kemot to dostaje blad w tej lini: this.countImage = ImageManager.getImages(new JPGFileFilter()).length;
java.lang.NullPointerException

0

Kurde co jest nie tak?

0

Nie znajduje ci żadnych plików i zwraca null zamiast tablicy. Potem wołasz null.length. Upewnij się, czy na pewno szukasz w odpowiednim katalogu. W dodatku to jest bez sensu:

if (jpgFiles.length > 0) {
    return jpgFiles;
}
return null;

Jak nie znajdzie żadnych, to chyba lepiej zwracać pustą tablicę niż potem dostawać NPE.

Propozycja metody dla JDK7:

public static DirectoryStream<Path> getImages() throws IOException {
    URI uri = null;
    try {
        uri = ChooseForm.class.getProtectionDomain().getCodeSource().getLocation().toURI();
    } catch (URISyntaxException ex) {
    }
    return Files.newDirectoryStream(Paths.get(uri).getParent(), "*.{jpg,jpeg}");
}

// ...

try (DirectoryStream<Path> images = ImageManager.getImages()) {
    for (Path image : images) {
        System.out.println(image);
    }
} catch (IOException ex) {
    ex.printStackTrace();
}

To, czy będzie wyłapywać xxx.JPG, xxx.JpEg jest zależne od implementacji, np. na windowsach znajduje.

0

Faktycznie zwracalo nulla używam javy 1.6 wiec zostalem przy starej wersji if (jpgFiles.length > 0) {
return jpgFiles;
}
return jpgFiles;

 Dzieki wielkie :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1