批处理文件的工具(java+shell为了实现)


有一堆语料须要处理一下才干使用,本来应该能够直接用shell脚本直接处理的。

可是对shell脚本不熟,仅仅会简单的一些命令。
因此就利用java+shell命令实现。

也许,直接用shell脚本处理是最好的。

也许你有什么绝妙的方法也请告诉我哦!

当然。我这个工具有个优点,就是假设通过shell命令实现不了的功能,能够用java实现,
加入对应接口就能够了。

工具里面的功能。Java负责调度,shell负责详细功能。
意思是说,我写的shell命令是针对单个文件操作的。java通过循环来调用那些shell命令,以此实现批处理。
眼下依据须要写了一些功能,比方字符串替换,文本头部或末尾加入内容。文本转码。

代码设计上。我留了一个叫Operation的接口,非常easy加入新的文本操作功能。




下面是源码:

  1. package com.linger.fileoperation;
  2.  
  3. public interface Operation
  4. {
  5. public void Run(String file,String[] options);
  6. }

  1. package com.linger.fileoperation;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.io.LineNumberReader;
  6.  
  7. public class Cmd {
  8.  
  9. /**
  10. * java调用shell命令的封装
  11. * 返回命令运行后的输出 //refer http://www.linuxidc.com/Linux/2012-04/58416.htm
  12. */
  13. public static String Run(String[] cmd, int tp) {
  14. StringBuffer buf = new StringBuffer(1000);
  15. String rt = "-1";
  16. try {
  17. Process pos = Runtime.getRuntime().exec(cmd);
  18. pos.waitFor();
  19. if (tp == 1) {
  20. if (pos.exitValue() == 0) {
  21. rt = "运行完成。";
  22. }
  23. } else {
  24. InputStreamReader ir = new InputStreamReader(
  25. pos.getInputStream());
  26. LineNumberReader input = new LineNumberReader(ir);
  27. String ln = "";
  28. while ((ln = input.readLine()) != null) {
  29. buf.append(ln + "\n");
  30. }
  31. rt = buf.toString();
  32. input.close();
  33. ir.close();
  34. }
  35. } catch (java.io.IOException e) {
  36. rt = e.toString();
  37. } catch (Exception e) {
  38. rt = e.toString();
  39. }
  40. return rt;
  41. }
  42.  
  43. public static void main(String[] args)
  44. {
  45. // TODO Auto-generated method stub
  46. //String[] commands = new String[] { "/bin/bash", "-c", "grep -r test *" };
  47. String[] commands = new String[] { "/bin/bash", "-c", "cd src;cd com;cd linger;cd fileoperation;ls" };
  48. //refer http://tuhaitao.iteye.com/blog/1047820
  49. String re= Cmd.Run(commands,-1);
  50. System.out.println(re);
  51. }
  52.  
  53. }

  1. package com.linger.fileoperation;
  2.  
  3. public class AddToDocHead implements Operation
  4. {
  5. //加入内容到文件头部
  6. //sed -i '1i<root>' t.txt
  7. //refer: http://zhidao.baidu.com/question/262964580.html
  8. @Override
  9. public void Run(String file, String[] options) {
  10. // TODO Auto-generated method stub
  11. String content = options[0];
  12. String[] commands = new String[] { "/bin/bash", "-c", "sed -i '1i"+content+"' "+file};
  13. String re= Cmd.Run(commands,1);
  14. System.out.println(re);
  15. }
  16. public static void main(String[] args) {
  17. // TODO Auto-generated method stub
  18. AddToDocHead rp = new AddToDocHead();
  19. String file = "/media/linger/G/sources/t.txt";
  20. String[] options = new String[]{"fuck"};
  21. rp.Run(file, options);
  22.  
  23. }
  24.  
  25. }
  1. package com.linger.fileoperation;
  2.  
  3. public class AddToDocTail implements Operation{
  4.  
  5. //加入内容到文本末尾
  6. @Override
  7. public void Run(String file, String[] options) {
  8. // TODO Auto-generated method stub
  9. String content = options[0];
  10. String[] commands = new String[] { "/bin/bash", "-c", "echo "+content+">>"+file};
  11. String re= Cmd.Run(commands,1);
  12. System.out.println(re);
  13. }
  14.  
  15. public static void main(String[] args)
  16. {
  17. // TODO Auto-generated method stub
  18. AddToDocTail rp = new AddToDocTail();
  19. String file = "/media/linger/G/sources/t.txt";
  20. String[] options = new String[]{"'</root>'"};
  21. rp.Run(file, options);
  22. }
  23.  
  24. }


  1. package com.linger.fileoperation;
  2.  
  3. //进入某个dir,把ls结果存到一个文件里
  4. public class LsDir implements Operation{
  5. @Override
  6. public void Run(String dir, String[] options) {
  7. // TODO Auto-generated method stub
  8. String fileName = options[0];
  9. String[] commands = new String[] { "/bin/bash", "-c", "cd "+dir+";ls>../"+fileName};
  10. String re= Cmd.Run(commands,1);
  11. System.out.println(re);
  12. }
  13.  
  14. public static void main(String[] args) {
  15. // TODO Auto-generated method stub
  16. LsDir ls = new LsDir();
  17. String[] options = new String[]{"sougou_news2008.ls"};
  18. String dir="/media/linger/G/sources/sougou_news2008";
  19. ls.Run(dir, options);
  20. }
  21.  
  22. }


  1. package com.linger.fileoperation;
  2.  
  3. public class Replace implements Operation
  4. {
  5. //字符串替换:将某个文件的全部 src换成dst
  6. @Override
  7. public void Run(String file,String[] options)
  8. {
  9. // TODO Auto-generated method stub
  10. String src = options[0];
  11. String dst = options[1];
  12. String[] commands = new String[] { "/bin/bash", "-c", "sed -i 's/"+src+"/"+dst+"/g' "+file};
  13. String re= Cmd.Run(commands,1);
  14. System.out.println(re);
  15.  
  16. }
  17.  
  18. public static void main(String[] args)
  19. {
  20. // TODO Auto-generated method stub
  21. Replace rp = new Replace();
  22. String file = "/media/linger/G/sources/t.txt";
  23. String[] options = new String[]{"&","&"};
  24. rp.Run(file, options);
  25. }
  26.  
  27. }


  1. package com.linger.fileoperation;
  2.  
  3. //转码:从gbk转为utf8
  4. public class TransCoding implements Operation{
  5. @Override //cat news.sohunews.010806.txt |iconv -f gbk -t utf8 -c>test.txt
  6. public void Run(String file, String[] options) {
  7. // TODO Auto-generated method stub
  8. String dst = options[0];
  9. String[] commands = new String[] { "/bin/bash", "-c", "cat "+file+" |iconv -f gbk -t utf8 -c>"+dst};
  10. String re= Cmd.Run(commands,1);
  11. System.out.println(re);
  12. }
  13.  
  14. public static void main(String[] args) {
  15. // TODO Auto-generated method stub
  16. TransCoding test = new TransCoding();
  17. String file = "/media/linger/G/sources/news.sohunews.010806.txt";
  18. String[] options = new String[]{"/media/linger/G/sources/t.txt"};
  19. test.Run(file, options);
  20. }
  21.  
  22. }

  1. package com.linger.fileoperation;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.RandomAccessFile;
  7. import java.util.ArrayList;
  8.  
  9. public class BatchOperation {
  10.  
  11. ///media/linger/G/sources/news.sohunews.010806.txt
  12. public static String path2Dir(String path)
  13. {
  14. int end = path.lastIndexOf('/');
  15. return path.substring(0, end);
  16. }
  17.  
  18. public static String path2FileName(String path)
  19. {
  20. int start = path.lastIndexOf('/')+1;
  21. int end = path.length();
  22. return path.substring(start, end);
  23. }
  24.  
  25. public static ArrayList<String> getFileList(String listFile) throws IOException
  26. {
  27. ArrayList<String> fileList = new ArrayList<String>();
  28. File file = new File(listFile);
  29. RandomAccessFile raf= new RandomAccessFile(file,"r");
  30. String line;
  31. while(true)
  32. {
  33. line = raf.readLine();
  34. if(line == null) break;
  35. fileList.add(line);
  36. }
  37. return fileList;
  38. }
  39.  
  40. public static void batchTransCoding() throws IOException
  41. {
  42. Operation oper = new TransCoding();
  43. String fileName;
  44. String Dir = "/media/linger/G/sources/sougou_news2008";
  45.  
  46. String[] options=new String[1];
  47. String newDir = "/media/linger/G/sources/sougou_news2008_utf8";
  48. ArrayList<String> fileList = getFileList("/media/linger/G/sources/sougou_news2008.ls");
  49.  
  50. for(int i=0;i<fileList.size();i++)
  51. {
  52. fileName = fileList.get(i);
  53. System.out.println(fileName);
  54. options[0] = newDir +"/" +fileName;
  55. oper.Run(Dir+"/"+fileName, options);
  56. }
  57. }
  58. public static void batchReplace() throws IOException
  59. {
  60. Operation oper = new Replace();
  61. String fileName;
  62. String Dir = "/media/linger/G/sources/sougou_news2008_utf8";
  63. String[] options = new String[]{"&","&"};
  64. ArrayList<String> fileList = getFileList("/media/linger/G/sources/sougou_news2008.ls");
  65.  
  66. for(int i=0;i<fileList.size();i++)
  67. {
  68. fileName = fileList.get(i);
  69. System.out.println(fileName);
  70. oper.Run(Dir+"/"+fileName, options);
  71.  
  72. }
  73. }
  74.  
  75. public static void batchAdd() throws IOException
  76. {
  77. Operation oper1 = new AddToDocHead();
  78. Operation oper2 = new AddToDocTail();
  79. String fileName;
  80. String Dir = "/media/linger/G/sources/sougou_news2008_utf8";
  81. String[] options1 = new String[]{"<root>"};
  82. String[] options2 = new String[]{"'</root>'"};//单引號能够避免转义
  83. ArrayList<String> fileList = getFileList("/media/linger/G/sources/sougou_news2008.ls");
  84.  
  85. for(int i=0;i<fileList.size();i++)
  86. {
  87. fileName = fileList.get(i);
  88. System.out.println(fileName);
  89.  
  90. //oper1.Run(Dir+"/"+fileName, options1);
  91. oper2.Run(Dir+"/"+fileName, options2);
  92.  
  93. }
  94. }
  95.  
  96. public static void main(String[] args) throws IOException
  97. {
  98. // TODO Auto-generated method stub
  99. batchAdd();
  100.  
  101. }
  102.  
  103. }


本文作者:linger
本文链接:http://blog.csdn.net/lingerlanlan/article/details/38515663



版权声明:本文博主原创文章,博客,未经同意不得转载。

批处理文件的工具(java+shell为了实现)的更多相关文章

  1. Java 9 揭秘(11. Java Shell)

    Tips 做一个终身学习的人. 在本章节中,主要介绍以下内容: 什么是Java shell JShell工具和JShell API是什么 如何配置JShell工具 如何使用JShell工具对Java代 ...

  2. 八、jdk工具之JvisualVM、JvisualVM之二--Java程序性能分析工具Java VisualVM

    目录 一.jdk工具之jps(JVM Process Status Tools)命令使用 二.jdk命令之javah命令(C Header and Stub File Generator) 三.jdk ...

  3. 简单的自动化运维工具(shell+except+whiptail+功能模块化函数+循环)

    简单的自动化运维工具(shell+except+whiptail+功能模块化函数+循环) http://www.cnblogs.com/M18-BlankBox/p/5881700.html

  4. 生产力工具:shell 与 Bash 脚本

    生产力工具:shell 与 Bash 脚本 作者:吴甜甜 个人博客网站: wutiantian.github.io 注意:本文只是我个人总结的学习笔记,不适合0基础人士观看. 参考内容: 王顶老师 l ...

  5. [日期工具分享][Shell]为特定命令依次传入顺序日期执行

    [日期工具分享][Shell]为特定命令依次传入顺序日期执行 使用方式: <本脚本文件名(必要时需要全路径)> <要执行的命令所在的文件名> <开始日期> < ...

  6. IDEA工具java.io.IOException: Could not find resource SqlMapConfig.xml

    IDEA工具java.io.IOException: Could not find resource SqlMapConfig.xml 解决办法: 1.删掉pom.xml文件的这行代码 <pac ...

  7. 001 发大招了 神奇的效率工具--Java代码转python代码

    今天发现一个好玩的工具: 可以直接将java转成python 1. 安装工具(windows 环境下面) 先下载antlr: 下载链接如下: http://www.antlr3.org/downloa ...

  8. Intellij IDEA工具Java web 环境搭建

    Java web 环境搭建 环境依赖 操作系统:Windows 7 64位 开发工具:IntelliJ IDEA 13.1.4 开发工具依赖环境 JDK版本:1.7+ 开发工具依赖插件 包管理:Mav ...

  9. [开发工具]Java开发常用的在线工具

    注明: 本文转自http://www.hollischuang.com/archives/1459.作为一个Java开发人员,经常要和各种各样的工具打交道,除了我们常用的IDE工具以外,其实还有很多工 ...

随机推荐

  1. storm原理介绍

    目录 storm原理介绍 一.原理介绍 二.配置 三.并行度 (一)storm拓扑的并行度可以从以下4个维度进行设置: (二)并行度的设置方法 (三)示例 四.分组 五.可靠性 (一)spout (二 ...

  2. windows关闭进程 批处理端口占用

    cmd 关闭进程java taskkill /F /IM java.exe taskkill /f /im java.exe 如何用dat批处理文件关闭某端口对应程序-Windows自动化命令 如何用 ...

  3. Xposed也要热更新

    好久没写博客了.这次玩一点不一样的. 吐槽&起因 相信熟悉Xposed的小伙伴们都知道,每次写完Xposed都要重新启动啊.有木有!反射错了,写错了名字.改一个log,都要重新启动啊有木有!重 ...

  4. 【Redis源代码剖析】 - Redis内置数据结构之压缩字典zipmap

    原创作品,转载请标明:http://blog.csdn.net/Xiejingfa/article/details/51111230 今天为大家带来Redis中zipmap数据结构的分析,该结构定义在 ...

  5. php获取调用本方法的上个方法,php堆栈,函数入库

    $array =debug_backtrace(); //print_r($array);//信息很齐全 unset($array[0]); foreach($array as $row) { $ht ...

  6. SpringMVC接受参数若干问题

    最近2年在工作问题总结中,好几次遇到了SpringMVC接收参数的问题,今天特别总结下.  SpringMVC接收参数的方法:  Html参数输入: <input name="stat ...

  7. RabbitMQ 服务

    RabbitMQ 使用场景一   安装环境 1.下载安装 Erlang 运行时环境 2.下载安装 RabbitMQ Server 应用程序 3.启动 RabbitMQ 服务(默认启动) 4.安装管理平 ...

  8. 前端Js框架汇总(工具多看)

    前端Js框架汇总(工具多看) 一.总结 一句话总结: 二.前端Js框架汇总 概述: 有些日子没有正襟危坐写博客了,互联网飞速发展的时代,技术更新迭代的速度也在加快.看着Java.Js.Swift在各领 ...

  9. 热烈庆祝UE4完全免费Free---GitHub上源码的关联方法

    声明:所有权利保留. 转载请说明出处:http://blog.csdn.net/cartzhang/article/details/44040317 IF YOU LOVE SOMETHING, SE ...

  10. OSGI简介—HelloWorld

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/wanghuan203/article/details/13631713 本次介绍的 HelloWor ...