Regular expressions are not supported in Java execution of Linux commands

Source: Internet
Author: User

Requirement background:

Corresponds the data in the database to the associated file path one by one. Such as:

Modify the File_path:/opt/downloads/20121105/001.pdf in table File_config to:/opt/downloads/20121107/001.pdf

This requires two steps, one is to update the database, and the other is to move the file in the database with the path consistent. This seems to be so simple, but in the actual operation of the process has found a problem, Java implementation of Linux commands (such as MV), does not support wildcard characters (such as. *), so you have to execute a MV command for each file.

The Execute MV command in Linux can support wildcard characters (regular expressions), such as: mv/opt/temp/srcpath/*.pdf/opt/temp/destpath/

is entirely possible. But the same operation is done to the Java runtime to perform.

Package com.mss.task;

Import Java.io.BufferedReader;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;

public class Testcomman {

	/**
	 * @param args
	 *
	/public static void main (string[] args) {
		String Srcpat h = "/opt/temp/srcpath/";
		String destpath = "/opt/temp/destpath/";
		
		
		String command = "mv" + Srcpath + args[0] + "  " +destpath;
		try {
			System.out.println ("Execute command is:" +command);
			Runtime Runtime = Runtime.getruntime ();
			Process process = runtime.exec (command);
			InputStream is = Process.getinputstream ();
			BufferedReader reader = new BufferedReader (new InputStreamReader (IS));
			String line = "";
			while (line = Reader.readline ())!=null) {
				System.out.println (line);
			}
		catch (Exception e) {
			E.printstacktrace ();}}


Perhaps the Java runtime has more advanced usage is also unknown, for the moment I did not find a better way to deal with, only one file corresponding to a MV command.

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.