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

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

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

可是对shell脚本不熟,仅仅会简单的一些命令。

因此就利用java+shell命令实现。

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

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

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

加入对应接口就能够了。

工具里面的功能。Java负责调度,shell负责详细功能。

意思是说,我写的shell命令是针对单个文件操作的。java通过循环来调用那些shell命令,以此实现批处理。

眼下依据须要写了一些功能,比方字符串替换,文本头部或末尾加入内容。文本转码。

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

下面是源码:

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' 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[]{"''"};

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 getFileList(String listFile) throws IOException

{

ArrayList fileList = new ArrayList();

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 fileList = getFileList("/media/linger/G/sources/sougou_news2008.ls");

for(int i=0;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 fileList = getFileList("/media/linger/G/sources/sougou_news2008.ls");

for(int i=0;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[]{""};

String[] options2 = new String[]{"''"};//单引號能够避免转义

ArrayList fileList = getFileList("/media/linger/G/sources/sougou_news2008.ls");

for(int i=0;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();

}

}

本文作者:linger

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值