工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 4137|回复: 23

选取文件中内容编程或脚本编写

[复制链接]
发表于 2007-10-25 21:12 | 显示全部楼层 |阅读模式
一个如 htm文件,里边有<head><body>,需要将<body>外的内容删除,<body>内的内容保留。。大家知道如何写么。。

用java,或者批量出黎脚本

发表于 2007-10-25 21:34 | 显示全部楼层
首先要保证<body>外的内容没有“<body>”,那读出整个文件到字符串,然后

str.substring(str.indexOf("<body>"),str.lastIndexOf("</body>"));
回复

使用道具 举报

 楼主| 发表于 2007-10-25 21:49 | 显示全部楼层
可否详细小小,真的不懂。。。。而且需要批量。。。

回复

使用道具 举报

 楼主| 发表于 2007-10-25 21:55 | 显示全部楼层
ps <body>  </body> 这两个tab也需要删除,只是留下 <body>  内的内容。。
回复

使用道具 举报

发表于 2007-10-25 22:41 | 显示全部楼层
LZ会用Java读写文件吗?

假设会,读一个文件的内容到一个字符串,剩下的事情就是对字符串操作了,然后写回到文件即可。

至于批量,递归读一个文件夹里的文件即可
回复

使用道具 举报

 楼主| 发表于 2007-10-25 22:55 | 显示全部楼层
<body>  </body> 这两个tab也需要删除 ,请问如何删除
回复

使用道具 举报

发表于 2007-10-25 23:14 | 显示全部楼层
str.substring(str.indexOf("<body")+6,str.lastIndexOf("</body>"));

如果6不合适,试试7或5、或1
回复

使用道具 举报

发表于 2007-10-26 00:11 | 显示全部楼层
个人推荐一个最高效既解决方式:稳2楼帮手搞掂就得了~至于请食饭或者比现金就睇LZ嘎啦~横掂一个月稳甘多钱,都唔争在分D比校友啦~
回复

使用道具 举报

 楼主| 发表于 2007-10-26 00:14 | 显示全部楼层
不耻问:

如何批量,循环读一个目录内的文件阿。。。。

回复

使用道具 举报

发表于 2007-10-26 00:46 | 显示全部楼层
严重同意8#  



  1. import java.util.*;
  2. import java.io.*;

  3. public class Test {
  4.    
  5.     public static void main(String[] args)throws Exception {
  6. getFiles(new File("E:\\java"));
  7. System.out.println(fileList.size());
  8.     }

  9.         private static List fileList=new ArrayList(159);

  10.         public static void getFiles(File file){
  11.                 if (file.isDirectory()) {
  12.                          // 获取目录下的文件和目录
  13.            File[] files = file.listFiles();
  14.            // 非空目录
  15.            if (files != null) {
  16.               for (int i = 0; i < files.length; i++) {
  17.                   getFiles(files);
  18.               }
  19.            }
  20.                 }else {
  21.                         fileList.add(file);
  22.                         System.out.println(file.getName());
  23.                 }
  24.         }
  25. }
复制代码

[ 本帖最后由 powerwind 于 2007-10-26 21:06 编辑 ]
回复

使用道具 举报

发表于 2007-10-26 01:17 | 显示全部楼层
LZ 是想实现文章采集功能吧???
回复

使用道具 举报

发表于 2007-10-26 09:37 | 显示全部楼层
丢到UNIX下写SHELL啦~~

LZ可以叫番碌碌宾宾佢地帮你写~哈哈哈~碌碌果D出手既话5分钟就帮你写好啦~
回复

使用道具 举报

 楼主| 发表于 2007-10-26 10:43 | 显示全部楼层
你叫我去死算喇

回复

使用道具 举报

 楼主| 发表于 2007-10-26 10:56 | 显示全部楼层
原帖由 powerwind 于 2007-10-25 23:14 发表
str.substring(str.indexOf("<body")+6,str.lastIndexOf("</body>"));

如果6不合适,试试7或5、或1





这样有一个大问题

如果<body>标签是<body leftmargin="0" topmargin="0">这种形式呢?
<script>
var str="sdff<body leftmargin=/"0/" topmargin=/"0/">这种形式呢?
回复

使用道具 举报

发表于 2007-10-26 11:07 | 显示全部楼层
碌碌宾宾,好名字
回复

使用道具 举报

 楼主| 发表于 2007-10-26 13:05 | 显示全部楼层
  1. /*
  2. * Created on Oct 26, 2007
  3. *
  4. * To change the template for this generated file go to
  5. * Window>Preferences>Java>Code Generation>Code and Comments
  6. */
  7. import java.io.*;

  8. /**
  9. * @author 43361813
  10. *
  11. * To change the template for this generated type comment go to
  12. * Window>Preferences>Java>Code Generation>Code and Comments
  13. */
  14. public class FiterContentOfHTML {

  15. public static void main(String[] args) throws IOException {

  16. File directory = new File(args[0]);

  17. if (directory.isDirectory()) {

  18. File targetDirectory = new File("Result_Of_Fitering");
  19. boolean targetDirExist = true;
  20. if (!targetDirectory.exists()) {
  21. targetDirExist = targetDirectory.mkdir();
  22. }

  23. String[] fileNames = directory.list();
  24. for (int i = 0; i < fileNames.length; i++) {
  25. String fileName = directory.getPath() + File.separator + fileNames;
  26. File file = new File(fileName);

  27. if (file.isFile()) {
  28. BufferedReader br = new BufferedReader(new FileReader(fileName));
  29. String oneLineOfContent = null;
  30. StringBuffer fullContent = new StringBuffer();
  31. boolean afterBodyTag = false;
  32. boolean bodyTagInMultiLines = false;
  33. while ((oneLineOfContent = br.readLine()) != null) {

  34. // For "<body>"
  35. if ((oneLineOfContent.toUpperCase().indexOf("") != -1) ||
  36. (bodyTagInMultiLines && oneLineOfContent.indexOf(">") != -1)) {

  37. afterBodyTag = true;
  38. bodyTagInMultiLines = false;
  39. fullContent.delete(0, fullContent.length());
  40. continue;
  41. }
  42. else if (oneLineOfContent.toUpperCase().indexOf("<BODY") !="-1)" {
  43. bodyTagInMultiLines = true;
  44. continue;
  45. }

  46. // For "</body>"
  47. int locationOfBodyTag = oneLineOfContent.toUpperCase().indexOf("");
  48. if (locationOfBodyTag != -1) {
  49. fullContent.append(oneLineOfContent.subSequence(0, locationOfBodyTag));
  50. //如果subSequence报错,可以用substring
  51. afterBodyTag = false;
  52. }

  53. if (afterBodyTag) {
  54. fullContent.append(oneLineOfContent);
  55. fullContent.append("\r\n");
  56. }
  57. }
  58. br.close();

  59. if (targetDirExist) {
  60. String targetFileName = targetDirectory.getPath() + File.separator + fileNames;
  61. BufferedWriter bw = new BufferedWriter(new FileWriter(targetFileName));
  62. bw.write("\r\n" +
  63. fullContent.toString());
  64. bw.close();
  65. }
  66. else {
  67. System.out.println("The target folder is not created! Please rerun the program! ^__^");
  68. }
  69. System.out.println(fileNames + " is filtered content completely! ^__^");
  70. }
  71. }
  72. }
  73. else {
  74. System.out.println("The directory name has something wrong! Please input once again! ^__^");
  75. }
  76. }
  77. }
复制代码

[ 本帖最后由 smallpig 于 2007-10-26 17:26 编辑 ]
回复

使用道具 举报

 楼主| 发表于 2007-10-26 16:07 | 显示全部楼层
java.sun.com

www.eclipse.org
回复

使用道具 举报

发表于 2007-10-26 17:04 | 显示全部楼层
自己动手,丰衣足食,
不用什么都让别人帮吧?
回复

使用道具 举报

 楼主| 发表于 2007-10-26 17:25 | 显示全部楼层

  1. /*
  2. * Created on Oct 26, 2007
  3. *
  4. * To change the template for this generated file go to
  5. * Window>Preferences>Java>Code Generation>Code and Comments
  6. */
  7. import java.io.*;
  8. /**
  9. * @author 43361813
  10. *
  11. * To change the template for this generated type comment go to
  12. * Window>Preferences>Java>Code Generation>Code and Comments
  13. */
  14. public class FiterContentOfHTML {
  15. public static void main(String[] args) throws IOException {
  16.   
  17.   File directory = new File(args[0]);
  18.   
  19.   if (directory.isDirectory()) {
  20.    
  21.    File targetDirectory = new File("Result_Of_Fitering");
  22.    boolean targetDirExist = true;
  23.    if (!targetDirectory.exists()) {
  24.     targetDirExist = targetDirectory.mkdir();
  25.    }
  26.    
  27.    String[] fileNames = directory.list();
  28.    for (int i = 0; i < fileNames.length; i++) {
  29.     String fileName = directory.getPath() + File.separator + fileNames[i];
  30.     File file = new File(fileName);
  31.    
  32.     if (file.isFile()) {
  33.      BufferedReader br = new BufferedReader(new FileReader(fileName));
  34.      String oneLineOfContent = null;
  35.      StringBuffer fullContent = new StringBuffer();
  36.      boolean afterBodyTag = false;
  37.      boolean bodyTagInMultiLines = false;
  38.      boolean commentInMultiLines = false;
  39.      while ((oneLineOfContent = br.readLine()) != null) {
  40.       
  41.       // For "<body>"
  42.       if ((oneLineOfContent.toUpperCase().indexOf("<BODY") != -1 && oneLineOfContent.indexOf(">") != -1) ||
  43.        (bodyTagInMultiLines && oneLineOfContent.indexOf(">") != -1)) {
  44.         
  45.        afterBodyTag = true;
  46.        bodyTagInMultiLines = false;
  47.        fullContent.delete(0, fullContent.length());
  48.        continue;
  49.       }
  50.       else if (oneLineOfContent.toUpperCase().indexOf("<BODY") != -1) {
  51.        bodyTagInMultiLines = true;
  52.        continue;
  53.       }
  54.       
  55.       // For "</body>"
  56.       int locationOfBodyTag = oneLineOfContent.toUpperCase().indexOf("</BODY>");
  57.       if (locationOfBodyTag != -1) {
  58.        fullContent.append(oneLineOfContent.substring(0, locationOfBodyTag));
  59.        afterBodyTag = false;
  60.       }
  61.       
  62.       if (afterBodyTag) {
  63.       
  64.        // For "<!-- -->"
  65.        int locationOfCommentBeginningTag = oneLineOfContent.indexOf("<!--");
  66.        int locationOfCommentEndingTag = oneLineOfContent.indexOf("-->");
  67.        if (locationOfCommentBeginningTag != -1 && locationOfCommentEndingTag != -1) {
  68.         fullContent.append(oneLineOfContent.substring(0, locationOfCommentBeginningTag));
  69.         continue;
  70.        }
  71.        else if (locationOfCommentBeginningTag != -1) {
  72.         fullContent.append(oneLineOfContent.substring(0, locationOfCommentBeginningTag));
  73.         commentInMultiLines = true;
  74.         continue;
  75.        }
  76.        else if (locationOfCommentEndingTag != -1) {
  77.         fullContent.append(oneLineOfContent.substring(locationOfCommentEndingTag + 3));
  78.         commentInMultiLines = false;
  79.         continue;
  80.        }
  81.       
  82.        if (!commentInMultiLines) {
  83.         fullContent.append(oneLineOfContent);
  84.         fullContent.append("\r\n");
  85.        }
  86.       }
  87.      }
  88.      br.close();
  89.      
  90.      if (targetDirExist) {
  91.       String targetFileName = targetDirectory.getPath() + File.separator + fileNames[i];
  92.       BufferedWriter bw = new BufferedWriter(new FileWriter(targetFileName));
  93.       bw.write("<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>\r\n" +
  94.         fullContent.toString());
  95.       bw.close();
  96.      }
  97.      else {
  98.       System.out.println("The target folder is not created! Please rerun the program! ^__^");
  99.      }
  100.      System.out.println(fileNames[i] + " is filtered content completely! ^__^");
  101.     }
  102.    }
  103.   }
  104.   else {
  105.    System.out.println("The directory name has something wrong! Please input once again! ^__^");
  106.   }
  107. }
  108. }

复制代码
回复

使用道具 举报

 楼主| 发表于 2007-10-26 17:25 | 显示全部楼层
感谢楼上各位得帮助

特别感谢PPT的大力支持
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2025-5-15 01:55

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表