- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 JFileChooser 从文件夹中选择所有文件
.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
但这不允许我选择文件夹,它只能让我打开它。那么我如何能够使用 JFileChooser 选择一个文件夹,然后输入所选文件夹中的所有文件,而不必实际单独选择每个文件,因为将来该文件夹中可能会有很多文件。我的整个代码如下所示
public class PicTest
{
public static void main(String args[])
{
File inFile,dir;
File[] list;
Image pic[] = new Image[50];
JFileChooser choose = new JFileChooser();
choose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int status = choose.showOpenDialog(null);
if(status == JFileChooser.APPROVE_OPTION)
{
dir = choose.getCurrentDirectory();
try
{
inFile = new File(choose.getSelectedFile().getAbsolutePath());
list = dir.listFiles();
for(int i=0; i < pic.length-1; i++)
{
BufferedImage buff = ImageIO.read(inFile);
pic[i] = buff;
}
}
catch(IOException e)
{
System.out.println("Error");
}
}
}
}
最佳答案
从你的代码来看,你似乎不需要。只需允许用户选择要处理的目录并使用 File#listFiles
获取其内容
然后,您将迭代此列表并读取每个文件,例如......
Image pic[] = null;
JFileChooser choose = new JFileChooser();
choose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int status = choose.showOpenDialog(null);
if (status == JFileChooser.APPROVE_OPTION) {
File dir = choose.getCurrentDirectory();
if (dir.exists()) {
File[] list = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
String name = pathname.getName().toLowerCase();
return name.endsWith(".png")
|| name.endsWith(".jpg")
|| name.endsWith(".jpeg")
|| name.endsWith(".bmp")
|| name.endsWith(".gif");
}
});
try {
// Only now do you know the length of the array
pic = new Image[list.length];
for (int i = 0; i < pic.length; i++) {
BufferedImage buff = ImageIO.read(list[i]);
pic[i] = buff;
}
} catch (IOException e) {
System.out.println("Error");
}
}
}
下面的简单代码允许我选择一个目录并单击打开,这将在请求时返回选择为所选文件的目录...
JFileChooser choose = new JFileChooser();
choose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (choose.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
System.out.println(choose.getSelectedFile());
}
关于java - 如何让 JFileChooser 将文件夹视为目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34301023/
将“”转换为“>”的主要目的是避免以下内联脚本: var foo = "alert('bug');"; // the value of foo is generated from server
我有以下想法: 在德语中我们有四个额外的字母(ä、ö、ü、ß),我不知道任何其他语言有这些声音,但我认为有口音的法国人也知道这个问题。我们在 Google Play 商店中有很多适用于城市、公交车站、
#!/bin/bash read nameArg find -name "$nameArg" 使用此代码,当我输入例如 *.txt 时,它将为我提供以 txt 结尾的所有文件,但我只想要名称为 *.t
我在 MySQL 5.7.27 中有一个带有 utf8mb4_unicode_ci 排序规则的用户表。 不幸的是, ı 没有像 i 那样进行线程化,例如,以下查询将找不到 Yılmaz select
我的简单 MySQL 查询: SELECT `word` FROM `nouns` WHERE `word` LIKE 'vandenys' 返回: vandenis 但是“vandenYs
虽然我以前用过这样的代码,而且很明显编译器有足够的信息可以工作,但我真的不明白为什么会这样编译: template auto foo(const T& t, I i) { return st
如何实现一个以 int 开头的函数,并在每次(经历有限数量的可能性)返回 1 的几个(例如,5 个) bool 值之一时从中减去 1。 理想情况下的外观是: function list1 list2
因此,如果我的数据库中有一个包含值的表 1 2 3 4 NULL 我执行了查询 SELECT MAX(col1) FROM 我会得到 4。有什么办法可以改变这个,所以 Null 将被视为最大值而不是
例如:我在数据库中有一条记录:[Example] Attena Illusive - 01 [720p].mkv尝试使用查询进行搜索: SELECT ts_rank_cd(to_tsvector('
我试图创建 2 个简单的盒子,并允许用户从 1 个盒子中选择数据,然后将其复制到第二个盒子。如果第二个框中已经有相同的文本,请在后面附加一些简单的文本。 它基本上在该项目不在第二个框中时起作用。但是,
这个问题已经有答案了: How to read a file from jar in Java? (6 个回答) 已关闭10 年前。 我想从我的 *jar 存档中读取文件。我在互联网上阅读了如何从 z
我在 Javascript 中偶然发现了一个我无法理解的极其奇怪的事件。 这是一个非常简单的 if 语句: let hours = 20; 我在这里设置了一个断点,并在调试器中设置了hours = 0
这两个查询给出了完全相同的结果: select * from topics where name='Harligt'; select * from topics where name='Härligt
我有一个包含数值和 NaN 的表格。求和时,如果所选值包含 NaN,则结果将为 NaN。有没有办法让 postgresql 在求和时将它们视为 0 而不是 NaN?或者我只需要将表中的所有 NaN 转
我有一个正在构建的页面,我希望当我滚动(向上或向下)页面时滚动到下一个 div(每个 div 是窗口高度的 100%)。并在那里“固定”,直到您再次滚动。可以在此处看到我正在努力完成的示例: http
我正在用 Javascript 制作一个小的 HTML 页面。它不需要服务器端,但我需要存储这个人所做的事情,所以我正在使用 localStorage。( list ) 如今,浏览器可以选择不存储 c
这两个查询给了我完全相同的结果: select * from topics where name='Harligt'; select * from topics where name='Härligt
我想向 Lua 公开一些 C++ 类。我可以调用Widget:New()获取带有元表集的返回用户数据到表 WidgetMeta . WidgetMeta包含所有 C++ 函数,它是 __index设置
我正在使用一个大型的旧数据库,现在我尝试使用 hibernate 而不是 SQL 来访问它。更大的问题之一是在外键中无限制地使用“0”和“-1”(意思是“NULL”)。 我生成了很多代码,但我手动添加
我试图将一个数字传递到一个 BYTES 数组中 - 但将该数字视为一个字符数组。这是我的代码: for(int i=1;i<=totalFiles;i++) { BYTE* input = n
我是一名优秀的程序员,十分优秀!