用Java读取多个文件
我想一次将多个文件读入Java。文件名如下:
- nnnnn_UM2012.txt
- ghkjdf_UM2045.txt
- erey_UM2189.txt
- ....
有超过1,000个文件,我不想用Java一一写下所有文件名,使用类似于以下代码的代码:
String fileNames = {"nnnnn_UM2012.txt","ghkjdf_UM2045.txt","erey_UM2189.txt", …}
也许文件名应该以相反的顺序阅读。我怎样才能做到这一点?
I want to read multiple files into Java at once. File names are like:
- nnnnn_UM2012.txt
- ghkjdf_UM2045.txt
- erey_UM2189.txt
- ....
There are over 1,000 files and I do not want to write all files names in Java one by one, using code similar to the following one:
String fileNames = {"nnnnn_UM2012.txt","ghkjdf_UM2045.txt","erey_UM2189.txt", …}
Maybe the filenames should be read in reverse order. How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
获取文件夹中的所有文件(文件列表中包含子文件夹):
获取文件夹中的所有文件(不包括子文件夹):
将文件列表按大小写相反的顺序排序:
对列表进行排序将文件转换为不区分大小写的反向顺序:
To get all files in a folder (sub-folders are included in the list of files):
To get all files in a folder, excluding sub-folders:
To sort the list of files into reverse case-sensitive order:
To sort the list of files into reverse case-insensitive order:
您可以使用 listFiles< /a> 方法获取文件夹中的所有文件。
You can use listFiles method to obtain all the files in the folder.
我想这可以解决你的问题。
I guess it can be a solution to your problem.
如果所有文件都在一个目录中,您可以遵循以下方法。
通过提供目录的完全限定路径来获取对目录的引用,然后使用
list()
函数将所有文件名获取到目录内的String
数组中。在此步骤之后,您可以根据您想要的方式对文件进行排序(例如按名称、长度等)。
You can follow the below approach if all the files are in a single directory.
Get a reference to the directory by providing its fully qualified path and then use the
list()
function get all the file names into aString
array inside the directory.After this step you can sort the files according to the way you want (For e.g. by its name,length,etc..).