`
wandejun1012
  • 浏览: 2693243 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

java process copy

    博客分类:
  • java
 
阅读更多

 

/**
	 * 
	 * @param sourceFile
	 *            待复制的源文件,必须用双反斜杠
	 * @param targetDir
	 *            待复制到的目录,必须用双反斜杠
	 * @return
	 * @throws Exception
	 */
	@Override
	public int copyFiles(String sourceFile, String targetDir) throws Exception {
		Runtime runtime = Runtime.getRuntime();
		int result;
		try {
			result = 0;
			StringBuilder sbStr = new StringBuilder();
			sbStr.append("xcopy /y /d  ").append(sourceFile).append("  ").append(targetDir).append("  ");
			Process process = runtime.exec(sbStr.toString());
			InputStream is = process.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			String line = null;
			StringBuffer sb = new StringBuffer();
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
			is.close();
			isr.close();
			br.close();
			if (null != sb && !sb.toString().equals("")) {
				String strResult = sb.toString();
				int copyIndex = strResult.indexOf("复制了");
				int startIndex = copyIndex + 3;
				int endIndex = copyIndex + 5;
				result = Integer
						.parseInt(strResult.substring(startIndex, endIndex).trim());
			}
		} catch (Exception e) {
			e.printStackTrace();
			result=0;
		}
		return result;
	}

 

 

public void testCopyMethod(){
		IMonitorService monitorService=new MonitorServiceImpl();
		String sourceFile="d:\\temp\\testcopy\\source\\*.*";
		String targetDir="d:\\temp\\testcopy\\target";
		try {
			int number=monitorService.copyFiles(sourceFile, targetDir);
			System.out.println("复制了"+number+"个文件");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

 

 

 

注意:源文件或者目的文件必须用双斜杠,用反斜杠不行。。。。

 

另外,中文字符串的长度其实和英文的计算方法一样。但是这里通过CMD调用返回后的值确实诡异,所以截取是[3,5).

 

RefUrl:http://stackoverflow.com/questions/592262/cmd-file-copy-from-java

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    ambari报错解决

    报错1: Using python /usr/bin/python ...REASON: Before starting Ambari Server, you must copy the MySQL JDBC driver JAR file to /usr/share/java and set property server.jdbc.driver.path=[path/to/c

    java编写的计算器,能实现加减乘除

    java编写的计算器,能实现加减乘除。 //***该梦幻计算器与Windows附件自带计算器的标准版功能、界面相仿***// //******但还不支持键盘操作,如有需要,下一次等级将满足你**********// import java.awt.*; import ...

    Java邮件开发Fundamentals of the JavaMail API

    Looking to incorporate mail facilities into your platform-independent Java solutions? Look no further than the JavaMail API, which offers a protocol-independent model for working with IMAP, POP, ...

    [JAVA性能瓶颈和漏洞检测].JProbe.Suite.v7.0

    Windows版安装说明:1. Install application. During installation ...2. Copy two jar files from crack.zip to <%JPROBE%>\lib directory. 3. Run jpprofiler.exe. When prompted import jprobe-suite.license file.

    带注释的Bootstrap.java

    You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is ...

    java8看不到源码-s2i-scala:Scala应用程序的图像来源

    process \ -f https://raw.githubusercontent.com/OutThereLabs/s2i-scala/master/buildconfig.yaml \ | oc create -f - 您还可以将 s2i Scala 用于 . 二进制构建 您可以通过附加源代码的 target/universal/stage ...

    apache-ant-1.7.1-bin.zip_Ant 1.7_Apache-Ant 1.7.1_ant1.7_apache

    are now able to process not only file system resources but also zip entries, tar entries, paths, ... Resource collections group resources, and can be further combined with operators such as union and...

    android-process-button

    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without r

    Axis2 code generator

    An error ocurred while completing process -java.lang.reflect.InvocationTargetException 是因为少了两个jar包: backport-util-concurrent-3.1.jar 包和geronimo-stax-api_1.0_spec-1.0.1.jar 包 [解决方案] ...

    JDK1.7 32位

    JDK1.7新特性介绍 1. 对Java集合(Collections)的增强支持 ...本文来自 烦人的萝卜籽 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/jiangliwe/article/details/76026546?utm_source=copy

    freemarker生成复杂word

    [java] view plaincopyprint? 1. package com.test.freemarker.report; 2. 3. 4. 5. import java.io.BufferedWriter; 6. 7. import java.io.File; 8. 9. import java.io.FileOutputStream; 10. ...

    backport-util-concurrent-3.1.jar geronimo-stax-api_1.0_spec-1.0.1.jar 下载

    An error ocCurred while completing process -java.lang.reflect.InvocationTargetException (1).关闭 Eclipse (2).copy %AXIS2_HOME%\lib\ 下的 backport-util-concurrent-3.1.jar 和 geronimo-stax-api_1.0_...

    leetcode中国-blog:学习笔记

    一、Java 1.Spring boot笔记: 程序员工具推荐 实用工具 mathpix:可以使截图变为LaTeX typora:markdown语言编辑器,可以随时预览,插入图片、代码预览等 secureCRT、xshell:好用的ssh工具,安装插件后可以快速传输...

    ffmpeg-20170620-ae6f6d4-win64

    comm.append(" ").append(" -vcodec copy -f flv -an rtmp://192.168.30.21/live/test2"); System.out.println(comm.toString()); return comm.toString(); } else { throw new RuntimeException(...

    移动代理源代码

    Copy there files: ./JMF2.1.1/lib/jmf.jar ./JMF2.1.1/lib/sound.jar to the directory: ./JSDK/jre/lib/ext. Note: these jar files contain the JMF classes used by the virtual machine at the runtime. ...

    ocp-sso:BCGov单点登录密钥

    oc process -f openshift/sso74-x509-postgresql-secrets.yaml -p 'NAME=template.sso' -p 'SUFFIX=' -l part-of=rh-sso,managed-by=template,shared=true | oc create -f -\noc process -f openshift/sso74-x509-...

    hadoop权威指南 第三版 英文版

    You can pre-order a copy, or buy the “Early Release” ebook today (you will receive the final ebook version when it is available for no extra charge). The following section is from the book’s ...

    Scala for the Impatient 2nd (完整英文第二版 带书签)

    xii Contents14.10 The copy Method and Named Parameters 205 14.11 Infix Notation in case Clauses 206 14.12 Matching Nested Structures 207 14.13 Are Case Classes Evil? 208 14.14 Sealed Classes 209 14.15...

    Github-volley 最新jar包和源代码 2015.07.28

    merge upstream, process response bodies only when present tweak getCacheKey(), using method and original url, fix #67 #78 add RedirectError, wrong exception for http 301 and 302, fix #51 make ...

Global site tag (gtag.js) - Google Analytics