刚刚学了IO,老师留了一些作业。同学想copy我的,又不好意思不给
于是想用File写个n层文件夹(n>5000) 然后把程序放在底层给他们
同时输出CDKey。txt为文件夹底层路径 方便自己管理
为了防止别人使用粘贴复制来叠加路径 所以使用的是随机文件名所有的设想就是这样程序也很简单import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2006-11-14
* Time: 3:12:08
* To change this template use File | Settings | File Templates.
*/
public class JokeDir {
public static void main(String[] args) throws IOException {
String strPath = "C:\\Documents and Settings\\Administrator\\桌面";
for (int i = 0; i < 5000; i++) {
int num = (int) (Math.random() * 1000);
strPath = strPath + "\\" + Integer.toString(num);
File f = new File(strPath);
f.mkdir();
}
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Documents and Settings\\Administrator\\桌面\\CDKey.txt"));
bw.write(strPath);
bw.close();
}
}之后就很郁闷了,先不说因为路径名太长而无法访问底层文件夹
就是想删除这个文件夹都不可能,从DOS下都不可以...
文件可以剪切,但是无法剪切到U盘有兴趣的话大家可以试试,并想想办法PS。有趣的是,这个无限文件夹可以打包成rar
只要在别电脑上一解压缩,那么这个文件夹就钉在那台电脑上了
我忽然想到可以做一个批处理,在别人的电脑上运行,让他们的桌面铺满删不掉的文件夹
于是想用File写个n层文件夹(n>5000) 然后把程序放在底层给他们
同时输出CDKey。txt为文件夹底层路径 方便自己管理
为了防止别人使用粘贴复制来叠加路径 所以使用的是随机文件名所有的设想就是这样程序也很简单import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2006-11-14
* Time: 3:12:08
* To change this template use File | Settings | File Templates.
*/
public class JokeDir {
public static void main(String[] args) throws IOException {
String strPath = "C:\\Documents and Settings\\Administrator\\桌面";
for (int i = 0; i < 5000; i++) {
int num = (int) (Math.random() * 1000);
strPath = strPath + "\\" + Integer.toString(num);
File f = new File(strPath);
f.mkdir();
}
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Documents and Settings\\Administrator\\桌面\\CDKey.txt"));
bw.write(strPath);
bw.close();
}
}之后就很郁闷了,先不说因为路径名太长而无法访问底层文件夹
就是想删除这个文件夹都不可能,从DOS下都不可以...
文件可以剪切,但是无法剪切到U盘有兴趣的话大家可以试试,并想想办法PS。有趣的是,这个无限文件夹可以打包成rar
只要在别电脑上一解压缩,那么这个文件夹就钉在那台电脑上了
我忽然想到可以做一个批处理,在别人的电脑上运行,让他们的桌面铺满删不掉的文件夹
http://photo.bababian.com/20061114/2A399F85BEDF46AC445BF2ADBC83F029_500.jpg以上是问题相关图片
映射成盘符,比如E,然后在E下面找到能够访问到的最深层的目录再映射成盘符F,这样重复几次就可
以删除了,当然删除的时候是从最后一个盘符进行的。祝你好运!
CSDN是一个人才辈出的地方!
哈哈~
我当年上学的时候最鄙视小气不给我作业抄的同学了!
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;public class JokeDir {
public static void main(String[] args){
String strPath = "D:\\jokedir";
for (int i = 0; i < 5000; i++) {
int num = (int) (Math.random() * 1000);
strPath = strPath + "\\" + Integer.toString(num);
File f = new File(strPath);
f.mkdirs();
}
BufferedWriter bw;
try {
bw = new BufferedWriter(new FileWriter("D:\\cdkey.txt"));
bw.write(strPath + "\\test.txt");
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
//delFolder(D:\\jokedir);
} public static void delFolder(String folderPath) {
try {
delAllFile(folderPath);
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete();
} catch (Exception e) {
e.printStackTrace();
}
} public static boolean delAllFile(String path) {
boolean bea = false;
File file = new File(path);
if (!file.exists()) {
return bea;
}
if (!file.isDirectory()) {
return bea;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);
delFolder(path + "/" + tempList[i]);
bea = true;
}
}
return bea;
}
}
加了两个递归删除文件夹的方法,试验过好用,请楼主引以为戒,不要再做这等事情了,个人意见,仅供参考。
将这句解除注释,并将参数用引号括起。
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2006-11-15
* Time: 4:00:01
* To change this template use File | Settings | File Templates.
*/
public class DeleteJokeDir {
static int i=0;
public static void main(String[] args) {
String strPath = "C:\\Documents and Settings\\Administrator\\桌面\\obj";
deleteDir(strPath);
} static void deleteDir(String strPath) {
File f = new File(strPath);
if(f.list().length!=0){
System.out.println("DOWN"+i++);
deleteDir(strPath+"\\"+f.list()[0]);
}
f.delete();
System.out.println("UP"+i--);
}
}