6

Here's what I have to do, but I have no idea where to start:

Write a program that allows you to browse images (gif, jpg) from the specified directory. Pictures are subsequently shown in the window, as follows:

  • a) the catalog and the time interval between image (in seconds) is determined at the start of the program on the basis of information from the file,
  • b) images are shown in their original sizes,
  • c) adjust the image to the frame

I know the very basic question, but just getting started with Java. Is there some sort of function, which will give me the names of all items in a folder?

acdcjunior
  • 132,397
  • 37
  • 331
  • 304
Daniel Zawadzki
  • 179
  • 1
  • 7
  • Take a look at this question for [listing](http://stackoverflow.com/questions/2056221/recursively-list-files-in-java) and this one for [filtering](http://stackoverflow.com/questions/2102952/listing-files-in-a-directory-matching-a-pattern-in-java) – A4L May 12 '13 at 18:14
  • Not sure if you want it, but I gave a functional template that you can work with, even if you intend to go deeper than just one folder in (ie if images can be located in sub-directories of the original path). – Steve P. May 12 '13 at 18:26

4 Answers4

7

If you want to have File Objects for all files within a Directory, use:

new File("path/to/directory").listFiles();

If you instead just want the names use

new File("path/to/directory").list();
Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72
3

If you want only the image files, you can use File.listFiles( FileFilter filter ):

 File[] files = new File( myPath ).listFiles( 
    new FileFilter() {
       boolean accept(File pathname) {
          String path = pathname.getPath();
          return ( path.endsWith(".gif") 
              || path.endsWith(".jpg")
              || ... );
       }
    });
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • I have another question. I have to specify the path to the file as "C: \ temp \ images", and the whole will be charged in another .txt file. Im going to read it by scanner and add, but that File[] wont accept path in this form. – Daniel Zawadzki May 12 '13 at 18:52
  • Backslash is special character in Java strings. You can escape it with a double-backslash. For example: "C:\\temp\\images." (Not sure that answers you're question; if not, please let me know.) – Andy Thomas May 12 '13 at 18:58
  • Not really, I mean that I get .txt file with the path to the folder with the pictures. The program needs to read a path through the scanner in the form of "C: \ temp \ images". The problem I have is how to provide the path to the program, because I wont see this path. All i get is the path to the first txt file. The rest has to be done by program. I hope you understand what I mean :( – Daniel Zawadzki May 12 '13 at 19:05
  • Okay. You need to be able to read a file, then use a path contained in that file to create a File object. You've got the first step -- you're breaking the big problem into smaller pieces. Now, tackle each of those separately. Bring separate, specific questions about them, if you have trouble, to StackOverflow. Once you get the path, you can create a new File object with *new File( myPath )*. – Andy Thomas May 12 '13 at 19:16
1

I assume that you want to get all images in a directory and all of its subdirectories. Here you go:

    //Load all the files from a folder.
    File folder = new File(folderPathString);
    readDirectory(folder);


public static void readDirectory(File dir) throws IOException
{        

    File[] folder = dir.listFiles();//lists all the files in a particular folder, includes directories

    for (int i = 0; i < folder.length; i++) 
    {
        File file = folder[i];

        if (file.isFile() && (file.getName().endsWith(".gif") || file.getName().endsWith(".jpg"))
        {
                read(file);
        }

        else if (file.isDirectory())
        {
                readDirectory(file);
        }

    }
}

public static void read(File input) throws IOException
{
        //Do whatever you need to do with the file  
}
Steve P.
  • 14,489
  • 8
  • 42
  • 72
1

If you can use JDK 7, then the recommended way (if I may say) is:

public static void main(String[] args) throws IOException {


    Path dir = Paths.get("c:/some_dir");
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.{gif,png,jpg}")) {
        for (Path entry: stream) {
            System.out.println(entry);
        }
    }

}

It is more efficient because you got iterator that not necessarily hold all the entries.

Boaz Nahum
  • 1,049
  • 8
  • 9