我使用 BufferedOutputStream 写入文件,它在指定的驱动程序中写入文件和目录列表,代码如下:
import java.io.File;
import java.nio.file.*;
import java.io.*;
import static java.nio.file.StandardOpenOption.*;
public class DirectoryReader {
static int spc_count=-1;
static void Process(File aFile)
{
String s;
Path file =
Paths.get("C:\\Java\\Files1.txt");
try
{
OutputStream output= new
BufferedOutputStream(Files.newOutputStream(file,CREATE));
spc_count++;
String spcs = "";
byte [] data= new byte [2048 * 2048];
for (int i = 0; i < spc_count; i++)
spcs += " ";
if(aFile.isFile())
{
System.out.println(spcs + "[FILE] " + aFile.getPath());
s=aFile.getPath();
System.out.println(" " + s);
data = s.getBytes();
output.write(data);
output.write(13);
output.write(10);
output.flush();
}
else if (aFile.isDirectory()) {
{
System.out.println(spcs + "[DIR] " + aFile.getPath());
s=aFile.getPath();
data = s.getBytes();
output.write(data);
output.write(13);
output.write(10);
output.flush();
}
File[] listOfFiles = aFile.listFiles();
if(listOfFiles!=null) {
for (int i = 0; i < listOfFiles.length; i++)
Process(listOfFiles[i]);
} else {
System.out.println(spcs + " [ACCESS DENIED]");
}
}
output.close();
spc_count--;
}
catch(Exception e)
{
System.out.println("Message: " + e);
}
}
public static void main(String[] args) {
String nam = "D:\\Notes";
File aFile = new File(nam);
Process(aFile);
}
}
但是当我去查看文件时,我只找到最后写入的文件,有时我会发现混词。