read files from a directory to a list

Osca

I'm new to Java and I stuck in a practice. The practice asked me to load the images from a directory and save them to a list. I have to complete the load method in the below code and not changing the loadLibrary method. I understood that I need to read the images to allShreds but I cannot figure out the right syntax. I also read the Java documentation but didn't find the examples useful.

import java.util.*;
import java.io.*;
import java.nio.file.*;
import javax.imageio.ImageIO;

public class DeShredder {
    private List<Shred> allShreds = new ArrayList<Shred>();

    public void loadLibrary(){
        Path filePath = Path.of(UIFileChooser.open("Choose first shred in directory"));
        Path directory = filePath.getParent(); //subPath(0, filePath.getNameCount()-1);
        int count=1;
        while(Files.exists(directory.resolve(count+".png"))){ count++; }
        count = count-1;
        load(directory, count); 
        display();
    }

    /** 
     * Empties out all the current lists (the list of all shreds, 
     * the working strip, and the completed strips). 
     * Loads the library of shreds into the allShreds list. 
     * Parameters are the directory containing the shred images and the number of shreds. * Each new Shred needs the directory and the number/id of the shred. 
     */
    public void load(Path dir, int count) {
        allShreds.clear();
    }
}

The Shred class

public class Shred{

    public static final double SIZE = 40;
    private String filename;
    private int id;            // ID of the shred

    public Shred(Path dir, int id){
        filename = dir.resolve(id+".png").toString();
        this.id = id;
    }

    public void draw(double left, double top){
        UI.drawImage(filename, left, top, SIZE, SIZE);
    }

    public void drawWithBorder(double left, double top){
        UI.drawImage(filename, left, top, SIZE, SIZE);
        UI.drawRect(left, top, SIZE, SIZE);
    }

    public String toString(){
        return "ID:"+id;
    }

}
UmFraWJ1bCBJc2xhbQ

In your example, allShreds is an ArrayList containing Shred objects. The correct way for adding objects to Arraylist is calling the add method with appropriate object. In your case, you need to add Shred object to the ArrayList.

And, from your question, by Loads the library of shreds into the allShreds list, it is meant that, you only need to load the Shred into the list. Your display method will then do the rest of the work for drawing and displaying images.

So, basically you just have to loop through all the image id in the directory and create new Shred from that image id and directory. And, add each of the Shred to the list:

public void load(Path dir, int count) {
    allShreds.clear();

    for (int i = 1; i <= count; i++) {
        allShreds.add(new Shred(dir, i));
    }
}

About the query from your comment:

Can you please also explain what does while in loadLibrary do ?

while(Files.exists(directory.resolve(count+".png"))){ count++; }

Here, it seems that, for the given test, in the given directory, always there need to have files in this sequence - 1.png, 2.png, 3.png, etc.

Now, inside the while loop, at first value of count is 1 and directory.resolve(count+".png") returns the absolute path of 1.png inside that directory. Then Files.exists() check if the path containing 1.png really exists, which means if the file 1.png really exists inside the given directory. If it exists, then the loop proceeds, the value of count becomes 2 and again it checks if 2.png exists in given directory and so on until it finds a [count].png which doesn't exist in that directory.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章