for(File singlefile:files){ if(singlefile.isDirectory() && !singlefile.isHidden()){ arrayList.addAll(findSong(singlefile)); } else{ if(singlefile.getName().endsWith(".mp3") || singlefile.getName().endsWith(".wav")){ arrayList.add(singlefile); } } }

时间: 2024-02-14 18:22:31 浏览: 19
这段代码看起来是一个递归函数,用于查找指定目录下的所有音乐文件(以 .mp3 或 .wav 结尾的文件)。它首先遍历指定目录下的所有文件和子目录,如果遍历到子目录就递归调用自己,将子目录中的音乐文件添加到结果列表中。如果遍历到的是文件而且是以 .mp3 或 .wav 结尾的音乐文件,就将其添加到结果列表中。最后将结果列表返回。
相关问题

finder.IsDirectory() && !finder.IsDots()C语言 什么意思

`finder.IsDirectory()` 和 `!finder.IsDots()` 是条件语句中的两个表达式,通常在C语言中用于检查目录中的条目是否是文件夹(目录)而不是文件,并且不是当前目录或父目录。 具体解释如下: - `finder.IsDirectory()`:这是一个函数或方法调用,用于检查`finder`对象所代表的目录条目是否为文件夹(目录)。如果返回值为真(非零),则表示该条目是一个文件夹;如果返回值为假(零),则表示该条目不是一个文件夹。 - `!finder.IsDots()`:这是一个逻辑否定操作符`!`与`finder.IsDots()`表达式的组合。`finder.IsDots()`函数用于检查`finder`对象所代表的目录条目是否为当前目录或父目录。如果返回值为真(非零),则表示该条目是当前目录或父目录;如果返回值为假(零),则表示该条目不是当前目录或父目录。逻辑否定操作符`!`会将`finder.IsDots()`的返回值取反,即如果`finder.IsDots()`返回真,则整个表达式的值为假,反之亦然。 因此,整个条件语句 `finder.IsDirectory() && !finder.IsDots()` 的含义是:当目录条目是文件夹(目录)且不是当前目录或父目录时,条件表达式的值为真(非零)。这样的条件通常用于在处理目录时过滤出真正的子目录或文件。

if (dir!=null&&dir.isDirectory()){ File[] files = dir.listFiles(); System.out.println(Arrays.toString(files)); for (File file : files) { if (file.isFile()&& file.getName().contains(s2)){ System.out.println("找到了,路径是"+file.getAbsolutePath()); } if (file!=null&&file.isDirectory()){ search(file,s2); } } }else{ System.out.println("您输入的不是文件夹"); }为什么空指针

可能会在两个地方产生空指针异常: 1. 当 `dir` 为 `null` 时,调用 `dir.isDirectory()` 会抛出空指针异常。 2. 在 `for` 循环中,如果 `files` 中存在为 `null` 的元素,调用 `file.isFile()` 或 `file.getName()` 会抛出空指针异常。 为了避免这些异常,应该在使用这些变量或方法之前,先进行非空判断。例如,可以在调用 `dir.isDirectory()` 之前,先判断 `dir` 是否为 `null`,如果是,则不执行后续代码。在 `for` 循环中,可以使用 `if` 判断 `file` 是否为 `null`,如果是,则跳过本次循环。

相关推荐

while (calendar .getTime().before(TUpdEndTm)) Date date = calendar .getTime(); calendar.add(Calendar .DAY_OF_YEARamount: 1) TUpdTm = sdf.format(date) ; String dir = rootdir + "/" + TUpdTm; // 连接FTP ChannelSftp connect = FtpUtil.connect(username, password, host, port);List<String> rootFilelists = FtpUtil.listFiles(connect, rootdir); if (!rootFilelists.isEmpty() && rootFilelists.contains(TUpdTm)) { try {vatService.deletefile( delpath: imageFile + "/" + TUpdTm);vatService.deletefile( delpath: imageFile + "/" + TUpdTm + ",zip");} catch (Exception e1) { e1.printStackTrace(); // 创建存放单证的文件夹 File cImFile = new File(imageFile);if (!clmFile.exists() && !clmFile.isDirectory()) {cImFile.mkdir(): // 创建以公式日期命名存放单证的文件夹 File clmNoFile = new File( pathname: imageFile + TUpdTm); if (!clmNoFile.exists() && !clmNoFile.isDirectory()) { clmNoFile.mkdir(); OutputStream os = null; try { List<String> fileLists = FtpUtil.listFiles(connect, dir);List<String> gsNumList = new ArrayList<String>(0); if (!fileLists.isEmpty()) { /*List<String> gsList = ps.getGsList(fileLists); for (String gs : gsList) { for (String file : fileLists) {if(file.contains(gs)){gsNumList.add(file); gsNumList = fileLists; if (!gsNumList.isEmpty()) { for (String file : gsNumList) { File localFile = new File( pathname: imageFile + TUpdTm + "/" + file);if (!localFile.getParentFile().exists()) { localFile.getParentFile().mkdirs(); TocalFile.createNewFile() connect.cd(dir);// 进入所在路径 os = new FileOutputStream(localFile); connect.get(file, os); os.close(); } else { serviceResponse.setReturn_type(ActionConstant.RESPONSE_RESULT_TYPE_ERROR);serviceResponse.setReturnMessage("该日期不存在公示异议!"); // /打包Zip vatService.createZip( sourcePath: imageFile + TUpdTm,zipPath: imageFile + TUpdTm + ".zip"); } catch (Exception e) { e.printStackTrace() : } finally { if (null != ftpFileInput) { ftpFileInput.close() : os.close(); l*//删除文件夹下所有文件 serviceResponse.setReturnMessage("true");} else { serviceResponse .setReturn_type(ActionConstant.RESPONSE_RESULT_TYPE_ERROR);serviceResponse.setReturnMessage(该日期不存在公示异议!"); return serviceResponse; 在这段代码中,TUpdTm会在while的循环中变成三个不同的值,如何能让只要有其中一次的TUpdTm满足if条件就不进入else,相反,三次TUpdTm的值均不满足if条件才进入else,不能使用break跳出循环的方法

以下代码存在读取文件乱码的情况请给与优化 public static void main(String[] args) { String sourceDirectory = "D:\Java\pas\trunk-5.X+\D05源代码\后台\达梦"; String destinationFile = "D:\Java\project\demo\src\main\resources\dm-init.sql"; try { // 创建输出文件,如果文件不存在则自动创建 File file = new File(destinationFile); if (!file.exists()) { file.createNewFile(); } // 打开输出流 FileWriter fw = new FileWriter(file.getAbsoluteFile(), true); BufferedWriter bw = new BufferedWriter(fw); // 递归读取源目录中的文件,并将其写入输出文件 readAndWriteFiles(sourceDirectory, bw); // 关闭输出流 bw.close(); System.out.println("文件已写入 " + destinationFile); } catch (IOException e) { e.printStackTrace(); } } private static void readAndWriteFiles(String sourceDirectory, BufferedWriter writer) throws IOException { // 创建目录文件对象 File directory = new File(sourceDirectory); // 检查目录是否存在并且是一个目录 if (!directory.exists() || !directory.isDirectory()) { throw new FileNotFoundException("目录不存在: " + sourceDirectory); } // 列出该目录下的所有文件和子目录,包括隐藏文件 File[] files = directory.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isFile() && (pathname.getName().endsWith(".sql") || pathname.getName().endsWith(".txt")); } }); // 遍历文件列表 for (File file : files) { // 读取文件内容并写入输出文件 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK")); String line = null; while ((line = br.readLine()) != null) { writer.write(line); writer.newLine(); } br.close(); } // 遍历目录中的子目录并递归读取 File[] subDirectories = directory.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory() && !pathname.isHidden(); } }); for (File subDirectory : subDirectories) { readAndWriteFiles(subDirectory.getAbsolutePath(), writer); } }

java.lang.IllegalArgumentException: Parameter 'directory' is not a directory at org.apache.commons.io.FileUtils.listFiles(FileUtils.java:293) at org.apache.commons.io.FileUtils.listFiles(FileUtils.java:378) at com.bosssoft.hr.train.j2se.util.UtilsDemo.method4(UtilsDemo.java:133) at Test1.testUtilsDemo4(Test1.java:66) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63) at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.ParentRunner.run(ParentRunner.java:413) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38) at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)源码如下:public void method4(){ // 获取目录中的所有文件和子目录 Collection<File> files = FileUtils.listFiles(new File("com/bosssoft/hr/train/j2se/util"), new String[]{"*.java"},true); // 遍历文件和子目录 if (files != null) { for (File file : files) { if (file.isDirectory()) { log.info("Directory: " + file.getName()); } else { log.info("File: " + file.getName()); } } } }

最新推荐

recommend-type

6-10.py

6-10
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这
recommend-type

导入numpy库,创建两个包含9个随机数的3*3的矩阵,将两个矩阵分别打印出来,计算两个数组的点积并打印出来。(random.randn()、dot()函数)

可以的,以下是代码实现: ```python import numpy as np # 创建两个包含9个随机数的3*3的矩阵 matrix1 = np.random.randn(3, 3) matrix2 = np.random.randn(3, 3) # 打印两个矩阵 print("Matrix 1:\n", matrix1) print("Matrix 2:\n", matrix2) # 计算两个数组的点积并打印出来 dot_product = np.dot(matrix1, matrix2) print("Dot product:\n", dot_product) ``` 希望
recommend-type

c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf

校园超市商品信息管理系统课程设计旨在帮助学生深入理解程序设计的基础知识,同时锻炼他们的实际操作能力。通过设计和实现一个校园超市商品信息管理系统,学生掌握了如何利用计算机科学与技术知识解决实际问题的能力。在课程设计过程中,学生需要对超市商品和销售员的关系进行有效管理,使系统功能更全面、实用,从而提高用户体验和便利性。 学生在课程设计过程中展现了积极的学习态度和纪律,没有缺勤情况,演示过程流畅且作品具有很强的使用价值。设计报告完整详细,展现了对问题的深入思考和解决能力。在答辩环节中,学生能够自信地回答问题,展示出扎实的专业知识和逻辑思维能力。教师对学生的表现予以肯定,认为学生在课程设计中表现出色,值得称赞。 整个课程设计过程包括平时成绩、报告成绩和演示与答辩成绩三个部分,其中平时表现占比20%,报告成绩占比40%,演示与答辩成绩占比40%。通过这三个部分的综合评定,最终为学生总成绩提供参考。总评分以百分制计算,全面评估学生在课程设计中的各项表现,最终为学生提供综合评价和反馈意见。 通过校园超市商品信息管理系统课程设计,学生不仅提升了对程序设计基础知识的理解与应用能力,同时也增强了团队协作和沟通能力。这一过程旨在培养学生综合运用技术解决问题的能力,为其未来的专业发展打下坚实基础。学生在进行校园超市商品信息管理系统课程设计过程中,不仅获得了理论知识的提升,同时也锻炼了实践能力和创新思维,为其未来的职业发展奠定了坚实基础。 校园超市商品信息管理系统课程设计的目的在于促进学生对程序设计基础知识的深入理解与掌握,同时培养学生解决实际问题的能力。通过对系统功能和用户需求的全面考量,学生设计了一个实用、高效的校园超市商品信息管理系统,为用户提供了更便捷、更高效的管理和使用体验。 综上所述,校园超市商品信息管理系统课程设计是一项旨在提升学生综合能力和实践技能的重要教学活动。通过此次设计,学生不仅深化了对程序设计基础知识的理解,还培养了解决实际问题的能力和团队合作精神。这一过程将为学生未来的专业发展提供坚实基础,使其在实际工作中能够胜任更多挑战。