批次檔工具(java+shell命令實現)

來源:互聯網
上載者:User

標籤:java   批次檔   shell   批處理   源碼   

批次檔工具(java+shell命令實現)

有一堆語料需要處理一下才能使用,本來應該可以直接用shell指令碼直接處理的。但是對shell指令碼不熟,只會簡單的一些命令。因此就利用java+shell命令實現。
也許,直接用shell指令碼處理是最好的。或許你有什麼絕妙的方法也請告訴我哦!當然,我這個工具有個好處,就是如果通過shell命令實現不了的功能,可以用java實現,添加相應介面就可以了。
工具裡面的功能,Java負責調度,shell負責具體功能。意思是說,我寫的shell命令是針對單個檔案操作的,java通過迴圈來調用那些shell命令,以此實現批處理。目前根據需要寫了一些功能,比如字串替換,文本頭部或末尾新增內容,文本轉碼。代碼設計上,我留了一個叫Operation的介面,很容易添加新的文本操作功能。



以下是原始碼:
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/1047820String 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@Overridepublic void Run(String file, String[] options) {// TODO Auto-generated method stubString 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 stubAddToDocHead 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{//新增內容到文本末尾@Overridepublic void Run(String file, String[] options) {// TODO Auto-generated method stubString 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 stubAddToDocTail 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{@Overridepublic void Run(String dir, String[] options) {// TODO Auto-generated method stubString 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 stubLsDir 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@Overridepublic void Run(String file,String[] options) {// TODO Auto-generated method stubString 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 stubReplace rp = new Replace();String file = "/media/linger/G/sources/t.txt";String[] options = new String[]{"&","&"};rp.Run(file, options);}}



package com.linger.fileoperation;//轉碼:從gbk轉為utf8public class TransCoding implements Operation{@Override  //cat news.sohunews.010806.txt |iconv -f gbk -t utf8 -c>test.txtpublic void Run(String file, String[] options) {// TODO Auto-generated method stubString 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 stubTransCoding 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.txtpublic 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 stubbatchAdd();}}



本文linger本文連結:http://blog.csdn.net/lingerlanlan/article/details/38515663


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.