Advertisement
darko13

Prva zad operativni(FileFilter)

Feb 26th, 2014
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1.  
  2. package filter;
  3.  
  4.  
  5. import java.io.File;
  6. import java.io.FilenameFilter;
  7.  
  8. public class Filter{
  9. public static void main(String[] args) {
  10. int i=0,zbir=0;
  11. float prosGol;
  12. File f = null;
  13. File[] filovi;
  14.  
  15. try{
  16. // create new file
  17. f = new File("C:\\");
  18.  
  19. // create new filename filter
  20. FilenameFilter fileNameFilter = new FilenameFilter() {
  21.  
  22. @Override
  23. public boolean accept(File dir, String name) {
  24. if(name.lastIndexOf('.')>0)
  25. {
  26. // get last index for '.' char
  27. int lastIndex = name.lastIndexOf('.');
  28.  
  29. // get extension
  30. String str = name.substring(lastIndex);
  31.  
  32. // match path name extension
  33. if(str.equals(".txt"))
  34. {
  35. return true;
  36. }
  37. }
  38. return false;
  39. }
  40. };
  41. // returns pathnames for files and directory
  42. filovi = f.listFiles(fileNameFilter);
  43.  
  44. // for each pathname in pathname array
  45. for(File k:filovi)
  46. {
  47. i++;
  48. zbir +=k.length();
  49. // prints file and directory paths
  50.  
  51. System.out.println(k);
  52. }
  53. prosGol=(float)zbir/i;
  54. System.out.println("Prosecna golemina"+prosGol);
  55. }catch(Exception e){
  56. // if any error occurs
  57. System.out.println("Error");
  58. }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement