列出指定目录的文件

import java.io.File; import java.io.IOException; public class FindDirectories { public static void main(String[] args) { if (args.length == 0) args = new String[]{".."}; try { File pathName = new File(args[0]); //if(!pathName.exists()) return; String[] fileNames = pathName.list();//列出所有的文件和名称 for (int i = 0; i < fileNames.length; i++) { File f = new File(pathName.getPath(), fileNames[i]); if (f.isDirectory()) {//撞南墙 System.out.println(f.getCanonicalPath());//是文件夹就打印其目录 main(new String[]{f.getPath()});//列出指定目录和文件 } } } catch (IOException e) { e.printStackTrace(); } } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Windows 开发中,您可以使用 FindFirstFile 和 FindNextFile 函数列出指定目录下的文件列表。下面是一个示例代码: ```cpp #include <windows.h> #include <iostream> #include <string> void ListFilesInDirectory(const std::wstring& directory) { std::wstring searchPath = directory + L"\\*.*"; WIN32_FIND_DATA findData; HANDLE hFind = FindFirstFile(searchPath.c_str(), &findData); if (hFind != INVALID_HANDLE_VALUE) { do { if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { // 跳过目录 continue; } std::wstring fileName = findData.cFileName; std::wcout << fileName << std::endl; } while (FindNextFile(hFind, &findData)); FindClose(hFind); } } int main() { // 指定目录路径 std::wstring directoryPath = L"C:\\Path\\To\\Directory"; ListFilesInDirectory(directoryPath); return 0; } ``` 在上面的代码中,我们定义了一个名为 `ListFilesInDirectory` 的函数,它接受一个目录路径作为参数。在该函数中,我们首先构建一个搜索路径,然后使用 `FindFirstFile` 函数打开搜索路径,并通过循环使用 `FindNextFile` 函数遍历目录中的文件。 在循环中,我们首先检查文件属性,如果是目录,则跳过。然后,我们获取文件名并将其输出到标准输出流 (`std::wcout`)。 在 `main` 函数中,我们指定列出文件目录路径,并调用 `ListFilesInDirectory` 函数进行列出。 请注意,上述代码仅列出目录中的文件,如果需要递归列出目录中的文件,请进行相应的递归操作。另外,为了正确处理 Unicode 路径,请使用 `std::wstring` 类型,并确保在编译时启用了 Unicode 字符集选项。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值