首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将文件从目录读取到列表

将文件从目录读取到列表
EN

Stack Overflow用户
提问于 2021-07-08 22:22:59
回答 2查看 108关注 0票数 2

我对Java很陌生,我一直在练习。实践要求我从目录中加载图像,并将它们保存到列表中。我必须在下面的代码中完成load方法,而不是更改loadLibrary方法。我知道我需要将图像读入allShreds,但我无法找到正确的语法。我还阅读了Java文档,但没有发现这些示例有用。

代码语言:javascript
复制
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();
    }
}

Shred

代码语言:javascript
复制
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;
    }

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-07-08 23:57:41

在您的示例中,allShreds是一个包含Shred对象的ArrayList。将对象添加到Arraylist的正确方法是使用适当的对象调用add方法。在您的示例中,需要将Shred对象添加到ArrayList中。

从您的问题中,对于Loads the library of shreds into the allShreds list,这意味着您只需要将Shred加载到列表中。然后,您的display方法将完成绘制和显示图像的其余工作。

因此,基本上,您只需循环遍历目录中的所有图像id,并从该图像id和目录创建新的Shred。然后,将每个Shred添加到列表中:

代码语言:javascript
复制
public void load(Path dir, int count) {
    allShreds.clear();

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

关于您的评论中的查询:

,你能解释一下在loadLibrary里做什么吗?

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

在这里,对于给定的测试,在给定的目录中,总是需要有这个顺序的文件-- 1.png2.png3.png等等。

现在,在while循环中,count的第一个值是1,directory.resolve(count+".png")在该目录中返回1.png的绝对路径。然后Files.exists()检查包含1.png的路径是否真的存在,这意味着文件1.png是否确实存在于给定的目录中。如果存在,则循环继续,计数的值变为2,并再次检查2.png是否存在于给定目录中,以此类推,直到找到该目录中不存在的[count].png

票数 1
EN

Stack Overflow用户

发布于 2021-07-09 00:26:25

这有点让人困惑。您的实践问题是:"..我必须在下面的代码中完成load方法,而不更改loadLibrary方法“。

  • 既不是静态的,也不是静态的。因此,假设是您使用DeShredder的方式:

代码语言:javascript
复制
DeShreder deShreder = new DeShreder();
deShreder.loadLibrary(); //This would call `load`

如果不更改

  • 方法,则目录的实际选择取决于loadLibrary。这意味着已经选择了目录。那么为什么load的签名包含dir呢?parameter?
  • Who决定如何编写Shred类的意义是什么?或者它是否已经编写了?如果要编写Shred类,是否应该包含错误处理,因为它可能无法解析文件?

还有几个问题,但我想你明白重点了。我认为这个做法(问题)对你来说还不清楚,或者这是相当不公平的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68309238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档