can';不要让endsWith(";.java";)工作
我正在遍历目录中的所有.jar文件,并希望在对话框中显示.jar文件名和.jar中的“.java”文件名。 我几乎是正确的,但是.jar中的所有文件名都会显示出来。 我的工作代码是:can';不要让endsWith(";.java";)工作,java,Java,我正在遍历目录中的所有.jar文件,并希望在对话框中显示.jar文件名和.jar中的“.java”文件名。 我几乎是正确的,但是.jar中的所有文件名都会显示出来。 我的工作代码是: for (Enumeration em1 = jarfile.entries(); em1.hasMoreElements();) { notes = Collections.list(em1); } JOptionPane.showMessageDialog(null, jarName + "\n" +
for (Enumeration em1 = jarfile.entries(); em1.hasMoreElements();)
{
notes = Collections.list(em1);
}
JOptionPane.showMessageDialog(null, jarName + "\n" + notes);
当我试图只删除(“.java”)文件名时,我无法正确地得到它
for (Enumeration em1 = jarfile.entries(); em1.hasMoreElements();)
{
if(em1.endsWith(".java"))
{
notes = Collections.list(em1);
}
}
JOptionPane.showMessageDialog(null, jarName + "\n" + notes);
我希望这是一件愚蠢的事情。我已经试了好几个小时了。有人能帮我把这个弄清楚吗?我想你想用
for (Enumeration<? extends ZipEntry> em1 = jarfile.entries();
em1.hasMoreElements();)
{
ZipEntry entry = em1.nextElement(); // Get a ZipEntry
if(entry.getName().endsWith(".java")) { // Get the name, should probably be
// ".class"
notes = Collections.list(em1); // It does end with ".java".
}
}
对于(枚举尝试将其转换为字符串
。这样您可以使用.endsWith(…)
方法:
String value = (String) em1.nextElement();
if(value.endsWith(".java"))
{
notes = Collections.list(em1);
}
谢谢大家给我的提示,没有一个答案是有效的。我最终使用了
ArrayList<String> javalist = new ArrayList<String>();
ZipFile zip = new ZipFile(jarName);
for (Enumeration list = zip.entries(); list.hasMoreElements(); )
{
ZipEntry entry = (ZipEntry) list.nextElement();
if(entry.getName().endsWith(".java"))
{
//below line is the answer
javalist.add(entry.getName());
}
JOptionPane.showMessageDialog(null, jarName + "\n" + javalist);
}
arraylistjavalist=newarraylist();
ZipFile zip=新ZipFile(jarName);
对于(枚举列表=zip.entries();列表.hasMoreElements();)
{
ZipEntry条目=(ZipEntry)list.nextElement();
if(entry.getName().endsWith(“.java”))
{
//下面一行是答案
add(entry.getName());
}
showMessageDialog(null,jarName+“\n”+javalist);
}
类型枚举
没有endsWith()
方法。首先,不需要在循环中调用collections.list。其次,枚举不是字符串,因此endsWith不适用于它。