Java中得getFiles
时间: 2024-02-03 14:13:13 浏览: 140
在Java中,可以使用以下代码获取指定目录下的所有文件:
```java
import java.io.File;
public class GetFilesExample {
public static void main(String[] args) {
File directory = new File("path/to/directory");
File[] files = directory.listFiles();
for (File file : files) {
if (file.isFile()) {
System.out.println(file.getName());
}
}
}
}
```
其中,`directory`表示要获取文件的目录,`listFiles()`方法返回一个`File`数组,包含目录中的所有文件和子目录。我们可以遍历这个数组并判断每个元素是否是一个文件,如果是文件则输出文件名。
相关问题
directory.GetFiles等价与Java中的啥
In Java, the equivalent of `Directory.GetFiles` would be the `File.listFiles()` method.
You can use it like this:
```java
File folder = new File(saveAudioFolder);
File[] audioFiles = folder.listFiles();
for (File audioFile : audioFiles) {
// do something with the audio file
}
```
This will return an array of `File` objects representing the files in the specified directory. You can then iterate over it to perform any required operations on the files.
Directory.GetFiles
Directory.GetFiles is a method in the .NET Framework that returns an array of file names in the specified directory that match the specified search pattern.
Syntax:
```
public static string[] GetFiles(string path, string searchPattern);
```
Parameters:
- path: the path to the directory to search
- searchPattern: the search pattern to match against the file names in the directory. This can include wildcards such as "*" and "?".
Return value:
- An array of file names that match the specified search pattern in the specified directory.
Example usage:
```
string[] files = Directory.GetFiles(@"C:\Users\Username\Documents", "*.txt");
foreach (string file in files)
{
Console.WriteLine(file);
}
```
This code will search for all files with a .txt extension in the user's Documents folder and print out the file names.
阅读全文