如何以递归方式列出Java中目录下的所有文件?框架是否提供任何实用程序?
我看到很多hacky实现.但没有来自框架或nio
可能重复:
使用Java递归列出目录中的所有文件
如何返回包含该文件夹上的所有文件的文件数组以及我的方法仅适用于文件夹的子文件夹,它不包含子文件夹.
public File[] listf(String directoryName) {
// .............list file
File directory = new File(directoryName);
// get all the files from a directory
File[] fList = directory.listFiles();
for (File file : fList) {
if (file.isFile()) {
System.out.println(file.getAbsolutePath());
} else if (file.isDirectory()) {
listf(file.getAbsolutePath());
}
}
System.out.println(fList);
return fList;
}
Run Code Online (Sandbox Code Playgroud) 我正在寻找Java7的新功能.我发现一个是 try-with-resources Statement.任何人都可以告诉我它究竟意味着什么?我们应该使用它的原因和位置,以及我们可以利用此功能的地方?即使是try声明也没有catch阻止让我感到困惑.
我想列出指定目录中的所有FILES和该目录中的子目录.不应列出任何目录.
我目前的代码如下.它无法正常工作,因为它只列出指定目录中的文件和目录.
我怎样才能解决这个问题?
final List<Path> files = new ArrayList<>();
Path path = Paths.get("C:\\Users\\Danny\\Documents\\workspace\\Test\\bin\\SomeFiles");
try
{
DirectoryStream<Path> stream;
stream = Files.newDirectoryStream(path);
for (Path entry : stream)
{
files.add(entry);
}
stream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
for (Path entry: files)
{
System.out.println(entry.toString());
}
Run Code Online (Sandbox Code Playgroud)
问候.
我想用
File f = new File("C:\\");
Run Code Online (Sandbox Code Playgroud)
使用该文件夹的内容创建一个ArrayList.
我对缓冲读者不是很好,所以请告诉我这是否更好.
这是我到目前为止的代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class buffered_read {
public static void main(String[] args) {
File f = new File("C:\\");
int x = 0;
boolean b = true;
File list[];
while(b = true){
}
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢,obiedog
请考虑以下代码:
Path directory = Paths.get(/* some directory */);
Files.list(directory).forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)
终端操作(如forEach)是否关闭已打开的基础文件?
请参阅Files.list的javadoc的相关部分:
返回的流封装了DirectoryStream.如果需要及时处理文件系统资源,则应使用try-with-resources构造来确保在流操作完成后调用流的close方法.
如果它没有调用Stream.close(),那么在生成可维护代码时调用它的最佳选择是什么?
我有一个包含文件类型对象的List.
Eg.List <File> copyFile = new ArrayList <File>();
现在我想将此copyFile复制到d:\\ demo \\ location.
AnyOne可以告诉我怎么做到这一点?
我知道如何将文件从路径复制到路径.
Files.copy(src, dst, StandardCopyOption.REPLACE_EXISTING);
Run Code Online (Sandbox Code Playgroud) 我知道用于Java中文件目录处理的Apache Commons IO库.但是,使用Java递归列出目录中的所有文件,讨论Java 7中的本机支持.
有没有人有过如何使用Java 7递归列出/读取目录中的文件的经验?
有一个 shell 脚本正在更新日志文件。
我想在写入日志文件时逐行显示日志(而不是一次显示整个文件)。
写入文件工作正常,但我在读取文件时遇到问题,因为它一次显示整个文件。
这是我的阅读代码:
String nmapstatusfile = runMT.instdir+"/logs/nmapout."+customer+".log";
String nmapcommand=runMT.instdir+"/bin/doscan.sh "+nmapoutfile+" "
+IPRange+" "+nmapstatusfile+" "+nmapdonefile;
System.out.println("nmapcommand is .. "+nmapcommand);
Runtime r = Runtime.getRuntime();
Process pr = r.exec(nmapcommand);
RandomAccessFile raf =null;
try {
System.out.println(nmapstatusfile);
System.out.println("Reading a file");
raf = new RandomAccessFile(nmapstatusfile, "r");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} // read from input file
System.out.println("Will print file contents line by line...");
long pos = 0;
raf.seek(pos);
String line =null;
while ((line = raf.readLine())!= null) …Run Code Online (Sandbox Code Playgroud)