Batch file tool (Java+shell command Implementation)

Source: Internet
Author: User

Batch file tool (Java+shell command implementation)

There are a bunch of corpora that need to be processed before they can be used, and should be directly handled directly with shell scripts. But not familiar with shell scripts, only a few simple commands. Therefore, it is implemented using the Java+shell command.
Perhaps it's best to use shell scripting directly. Perhaps you have any wonderful method also please tell me Oh! Of course, I have the advantage of this tool, is that if the shell command can not be implemented by the function, the Java implementation, add the corresponding interface on it.
Functions inside the tool, Java is responsible for scheduling, the shell is responsible for the specific functions. This means that the shell command I wrote is for a single file operation, and Java uses loops to invoke those shell commands to implement batching. At present, some functions are written according to need, such as string substitution, Text header or end add content, text transcoding. Code design, I left a call operation interface, it is easy to add new text operation function.



Here is the source code:
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 calls Shell command encapsulation * Returns output//refer after command execution 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);p os.waitfor (); if (tp = = 1) {if (pos.exitvalue () = = 0) { RT = "Execution Complete!" ";}} 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{//add content to file header//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{//add content to the end of text @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;//enters a dir and saves the LS result in a file 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{//string substitution: Convert all src for a file to 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;//transcoding: Convert from GBK to 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.S Ubstring (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> '"};//single quotes to avoid escaping 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 ();}}



This article linger this article link: http://blog.csdn.net/lingerlanlan/article/details/38515663


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.