public static void main(String[] args)
{
try
{
File dir = new File("D:\\WayneProject\\Logs");
if(dir.isDirectory())
{
for(File child: dir.listFiles()) //NOT WORKING AFTER 1 ITERATION
{
if(child.isFile())
{
String currentFile = child.getName();
String[] fileOutput = currentFile.split("\\.");
processFile(currentFile,fileOutput[0]);
}
}
}
}
请检查评论。即使文件在目录中,迭代文件也会给出 File not found 异常(对于第二次迭代)。你能告诉我为什么吗?谢谢
我的其他功能。fileOutput 用于设置目标文件的名称:
public static void processFile(String fileName, String fileOutput)
{
try
{
BufferedReader br = new BufferedReader(new FileReader(fileName));
String str = null;
File fileDest1 = new File("D:\\" + fileOutput + "1.csv");
BufferedWriter wr1 = new BufferedWriter(new FileWriter(fileDest1));
File fileDest2 = new File("D:\\" + fileOutput + "2.csv");
BufferedWriter wr2 = new BufferedWriter(new FileWriter(fileDest2));
wr1.write("Date, Memory Free\n");
wr2.write("Date, %Idle\n");
while((str=br.readLine()) != null)
{
String[] st = str.split("\\s+");
if (st[0].equals("MemFree:"))
{
wr1.write(st[1] + ",\n");
}
if(isDouble(st))
{
wr2.write(st[6] + "," + "\n");
}
if(isDate(st[0]))
{
String subStr = str.substring(0, 20);
wr1.write(subStr + ",");
wr2.write(subStr + ",");
}
}
br.close();
wr1.close();
wr2.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}