Batch File tool (java + shell command implementation)

Source: Internet
Author: User
Tags call shell

Batch File tool (java + shell command implementation)
Batch File tool (java + shell command implementation)

A bunch of corpus needs to be processed before it can be used. It should have been directly processed using shell scripts. But not familiar with shell scripts, only some simple commands. Therefore, the java + shell command is used.
Maybe it is best to directly use shell scripts to process the data. Maybe you have any wonderful methods. Please let me know! Of course, the advantage of this tool is that if the function cannot be implemented through shell commands, You can implement it in java and add corresponding interfaces.
Java is responsible for scheduling and shell is responsible for specific functions. It means that the shell commands I wrote are for operations on a single file. java calls those shell commands cyclically to implement batch processing. Currently, some functions are provided as needed, such as string replacement, adding content to the text header or end, and text transcoding. In code design, I have left an Operation called Operation, which makes it easy to add new text Operation functions.



The source code is as follows:

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 call shell command encapsulation * return the output after Command Execution // 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(cmd.exe c (cmd); pos. w AitFor (); if (tp = 1) {if (pos. exitValue () = 0) {rt = "execution completed! ";}} 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 the file header // sed-I '1i
 
  
'T.txt // refer: the 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 the content to the end of the 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 [] {"''"}; Rp. Run (file, options );}}



Package com. linger. fileoperation; // enter a dir and save the ls result to 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 replacement: Replace all the src of a file with 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.txt public 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.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 criteria = 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
     
      
FileList = getFileList ("/media/linger/G/sources/sougou_news2008.ls"); for (int I = 0; I
      
        "}; String [] options2 = new String [] {"''"}; // Single quotes can avoid escaping ArrayList
       
         FileList = getFileList ("/media/linger/G/sources/sougou_news2008.ls"); for (int I = 0; I
        
         


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


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.