java 字节流——把所有文件及目录后缀改成exe,并隐藏原文件

该博客介绍了如何使用Java编程实现将指定目录下所有文件及子目录的后缀名改为.exe,并隐藏原始文件。在D: ewDir路径下,包括图片文件和文件夹内的图片都被作为示例进行了处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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路径下有图片和文件夹里的图片
在这里插入图片描述
运行后:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值