批处理文件的工具(java+shell为了实现)
也许你有什么绝妙的方法也请告诉我哦!
- package com.linger.fileoperation;
- public interface Operation
- {
- public void Run(String file,String[] options);
- }
- package com.linger.fileoperation;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.LineNumberReader;
- public class Cmd {
- /**
- * java调用shell命令的封装
- * 返回命令运行后的输出 //refer http://www.linuxidc.com/Linux/2012-04/58416.htm
- */
- public static String Run(String[] cmd, int tp) {
- StringBuffer buf = new StringBuffer(1000);
- String rt = "-1";
- try {
- Process pos = Runtime.getRuntime().exec(cmd);
- pos.waitFor();
- if (tp == 1) {
- if (pos.exitValue() == 0) {
- rt = "运行完成。";
- }
- } else {
- InputStreamReader ir = new InputStreamReader(
- pos.getInputStream());
- LineNumberReader input = new LineNumberReader(ir);
- String ln = "";
- while ((ln = input.readLine()) != null) {
- buf.append(ln + "\n");
- }
- rt = buf.toString();
- input.close();
- ir.close();
- }
- } catch (java.io.IOException e) {
- rt = e.toString();
- } catch (Exception e) {
- rt = e.toString();
- }
- return rt;
- }
- public static void main(String[] args)
- {
- // TODO Auto-generated method stub
- //String[] commands = new String[] { "/bin/bash", "-c", "grep -r test *" };
- String[] commands = new String[] { "/bin/bash", "-c", "cd src;cd com;cd linger;cd fileoperation;ls" };
- //refer http://tuhaitao.iteye.com/blog/1047820
- String re= Cmd.Run(commands,-1);
- System.out.println(re);
- }
- }
- package com.linger.fileoperation;
- public class AddToDocHead implements Operation
- {
- //加入内容到文件头部
- //sed -i '1i<root>' t.txt
- //refer: http://zhidao.baidu.com/question/262964580.html
- @Override
- public void Run(String file, String[] options) {
- // TODO Auto-generated method stub
- String content = options[0];
- String[] commands = new String[] { "/bin/bash", "-c", "sed -i '1i"+content+"' "+file};
- String re= Cmd.Run(commands,1);
- System.out.println(re);
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- AddToDocHead rp = new AddToDocHead();
- String file = "/media/linger/G/sources/t.txt";
- String[] options = new String[]{"fuck"};
- rp.Run(file, options);
- }
- }
- package com.linger.fileoperation;
- public class AddToDocTail implements Operation{
- //加入内容到文本末尾
- @Override
- public void Run(String file, String[] options) {
- // TODO Auto-generated method stub
- String content = options[0];
- String[] commands = new String[] { "/bin/bash", "-c", "echo "+content+">>"+file};
- String re= Cmd.Run(commands,1);
- System.out.println(re);
- }
- public static void main(String[] args)
- {
- // TODO Auto-generated method stub
- AddToDocTail rp = new AddToDocTail();
- String file = "/media/linger/G/sources/t.txt";
- String[] options = new String[]{"'</root>'"};
- rp.Run(file, options);
- }
- }
- package com.linger.fileoperation;
- //进入某个dir,把ls结果存到一个文件里
- public class LsDir implements Operation{
- @Override
- public void Run(String dir, String[] options) {
- // TODO Auto-generated method stub
- String fileName = options[0];
- String[] commands = new String[] { "/bin/bash", "-c", "cd "+dir+";ls>../"+fileName};
- String re= Cmd.Run(commands,1);
- System.out.println(re);
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- LsDir ls = new LsDir();
- String[] options = new String[]{"sougou_news2008.ls"};
- String dir="/media/linger/G/sources/sougou_news2008";
- ls.Run(dir, options);
- }
- }
- package com.linger.fileoperation;
- public class Replace implements Operation
- {
- //字符串替换:将某个文件的全部 src换成dst
- @Override
- public void Run(String file,String[] options)
- {
- // TODO Auto-generated method stub
- String src = options[0];
- String dst = options[1];
- String[] commands = new String[] { "/bin/bash", "-c", "sed -i 's/"+src+"/"+dst+"/g' "+file};
- String re= Cmd.Run(commands,1);
- System.out.println(re);
- }
- public static void main(String[] args)
- {
- // TODO Auto-generated method stub
- Replace rp = new Replace();
- String file = "/media/linger/G/sources/t.txt";
- String[] options = new String[]{"&","&"};
- rp.Run(file, options);
- }
- }
- package com.linger.fileoperation;
- //转码:从gbk转为utf8
- public class TransCoding implements Operation{
- @Override //cat news.sohunews.010806.txt |iconv -f gbk -t utf8 -c>test.txt
- public void Run(String file, String[] options) {
- // TODO Auto-generated method stub
- String dst = options[0];
- String[] commands = new String[] { "/bin/bash", "-c", "cat "+file+" |iconv -f gbk -t utf8 -c>"+dst};
- String re= Cmd.Run(commands,1);
- System.out.println(re);
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- TransCoding test = new TransCoding();
- String file = "/media/linger/G/sources/news.sohunews.010806.txt";
- String[] options = new String[]{"/media/linger/G/sources/t.txt"};
- test.Run(file, options);
- }
- }
- package com.linger.fileoperation;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- import java.util.ArrayList;
- public class BatchOperation {
- ///media/linger/G/sources/news.sohunews.010806.txt
- public static String path2Dir(String path)
- {
- int end = path.lastIndexOf('/');
- return path.substring(0, end);
- }
- public static String path2FileName(String path)
- {
- int start = path.lastIndexOf('/')+1;
- int end = path.length();
- return path.substring(start, end);
- }
- public static ArrayList<String> getFileList(String listFile) throws IOException
- {
- ArrayList<String> fileList = new ArrayList<String>();
- File file = new File(listFile);
- RandomAccessFile raf= new RandomAccessFile(file,"r");
- String line;
- while(true)
- {
- line = raf.readLine();
- if(line == null) break;
- fileList.add(line);
- }
- return fileList;
- }
- public static void batchTransCoding() throws IOException
- {
- Operation oper = new TransCoding();
- String fileName;
- String Dir = "/media/linger/G/sources/sougou_news2008";
- String[] options=new String[1];
- String newDir = "/media/linger/G/sources/sougou_news2008_utf8";
- ArrayList<String> fileList = getFileList("/media/linger/G/sources/sougou_news2008.ls");
- for(int i=0;i<fileList.size();i++)
- {
- fileName = fileList.get(i);
- System.out.println(fileName);
- options[0] = newDir +"/" +fileName;
- oper.Run(Dir+"/"+fileName, options);
- }
- }
- public static void batchReplace() throws IOException
- {
- Operation oper = new Replace();
- String fileName;
- String Dir = "/media/linger/G/sources/sougou_news2008_utf8";
- String[] options = new String[]{"&","&"};
- ArrayList<String> fileList = getFileList("/media/linger/G/sources/sougou_news2008.ls");
- for(int i=0;i<fileList.size();i++)
- {
- fileName = fileList.get(i);
- System.out.println(fileName);
- oper.Run(Dir+"/"+fileName, options);
- }
- }
- public static void batchAdd() throws IOException
- {
- Operation oper1 = new AddToDocHead();
- Operation oper2 = new AddToDocTail();
- String fileName;
- String Dir = "/media/linger/G/sources/sougou_news2008_utf8";
- String[] options1 = new String[]{"<root>"};
- String[] options2 = new String[]{"'</root>'"};//单引號能够避免转义
- ArrayList<String> fileList = getFileList("/media/linger/G/sources/sougou_news2008.ls");
- for(int i=0;i<fileList.size();i++)
- {
- fileName = fileList.get(i);
- System.out.println(fileName);
- //oper1.Run(Dir+"/"+fileName, options1);
- oper2.Run(Dir+"/"+fileName, options2);
- }
- }
- public static void main(String[] args) throws IOException
- {
- // TODO Auto-generated method stub
- batchAdd();
- }
- }
版权声明:本文博主原创文章,博客,未经同意不得转载。
批处理文件的工具(java+shell为了实现)的更多相关文章
- Java 9 揭秘(11. Java Shell)
Tips 做一个终身学习的人. 在本章节中,主要介绍以下内容: 什么是Java shell JShell工具和JShell API是什么 如何配置JShell工具 如何使用JShell工具对Java代 ...
- 八、jdk工具之JvisualVM、JvisualVM之二--Java程序性能分析工具Java VisualVM
目录 一.jdk工具之jps(JVM Process Status Tools)命令使用 二.jdk命令之javah命令(C Header and Stub File Generator) 三.jdk ...
- 简单的自动化运维工具(shell+except+whiptail+功能模块化函数+循环)
简单的自动化运维工具(shell+except+whiptail+功能模块化函数+循环) http://www.cnblogs.com/M18-BlankBox/p/5881700.html
- 生产力工具:shell 与 Bash 脚本
生产力工具:shell 与 Bash 脚本 作者:吴甜甜 个人博客网站: wutiantian.github.io 注意:本文只是我个人总结的学习笔记,不适合0基础人士观看. 参考内容: 王顶老师 l ...
- [日期工具分享][Shell]为特定命令依次传入顺序日期执行
[日期工具分享][Shell]为特定命令依次传入顺序日期执行 使用方式: <本脚本文件名(必要时需要全路径)> <要执行的命令所在的文件名> <开始日期> < ...
- IDEA工具java.io.IOException: Could not find resource SqlMapConfig.xml
IDEA工具java.io.IOException: Could not find resource SqlMapConfig.xml 解决办法: 1.删掉pom.xml文件的这行代码 <pac ...
- 001 发大招了 神奇的效率工具--Java代码转python代码
今天发现一个好玩的工具: 可以直接将java转成python 1. 安装工具(windows 环境下面) 先下载antlr: 下载链接如下: http://www.antlr3.org/downloa ...
- Intellij IDEA工具Java web 环境搭建
Java web 环境搭建 环境依赖 操作系统:Windows 7 64位 开发工具:IntelliJ IDEA 13.1.4 开发工具依赖环境 JDK版本:1.7+ 开发工具依赖插件 包管理:Mav ...
- [开发工具]Java开发常用的在线工具
注明: 本文转自http://www.hollischuang.com/archives/1459.作为一个Java开发人员,经常要和各种各样的工具打交道,除了我们常用的IDE工具以外,其实还有很多工 ...
随机推荐
- storm原理介绍
目录 storm原理介绍 一.原理介绍 二.配置 三.并行度 (一)storm拓扑的并行度可以从以下4个维度进行设置: (二)并行度的设置方法 (三)示例 四.分组 五.可靠性 (一)spout (二 ...
- windows关闭进程 批处理端口占用
cmd 关闭进程java taskkill /F /IM java.exe taskkill /f /im java.exe 如何用dat批处理文件关闭某端口对应程序-Windows自动化命令 如何用 ...
- Xposed也要热更新
好久没写博客了.这次玩一点不一样的. 吐槽&起因 相信熟悉Xposed的小伙伴们都知道,每次写完Xposed都要重新启动啊.有木有!反射错了,写错了名字.改一个log,都要重新启动啊有木有!重 ...
- 【Redis源代码剖析】 - Redis内置数据结构之压缩字典zipmap
原创作品,转载请标明:http://blog.csdn.net/Xiejingfa/article/details/51111230 今天为大家带来Redis中zipmap数据结构的分析,该结构定义在 ...
- php获取调用本方法的上个方法,php堆栈,函数入库
$array =debug_backtrace(); //print_r($array);//信息很齐全 unset($array[0]); foreach($array as $row) { $ht ...
- SpringMVC接受参数若干问题
最近2年在工作问题总结中,好几次遇到了SpringMVC接收参数的问题,今天特别总结下. SpringMVC接收参数的方法: Html参数输入: <input name="stat ...
- RabbitMQ 服务
RabbitMQ 使用场景一 安装环境 1.下载安装 Erlang 运行时环境 2.下载安装 RabbitMQ Server 应用程序 3.启动 RabbitMQ 服务(默认启动) 4.安装管理平 ...
- 前端Js框架汇总(工具多看)
前端Js框架汇总(工具多看) 一.总结 一句话总结: 二.前端Js框架汇总 概述: 有些日子没有正襟危坐写博客了,互联网飞速发展的时代,技术更新迭代的速度也在加快.看着Java.Js.Swift在各领 ...
- 热烈庆祝UE4完全免费Free---GitHub上源码的关联方法
声明:所有权利保留. 转载请说明出处:http://blog.csdn.net/cartzhang/article/details/44040317 IF YOU LOVE SOMETHING, SE ...
- OSGI简介—HelloWorld
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/wanghuan203/article/details/13631713 本次介绍的 HelloWor ...