java调用windows系统的批处理(.bat文件) 和 linux系统的shell脚本(.sh文件)
2014-01-19 12:31
1011 查看
在批量拷贝系统下的文件时如果用java的IO流来读写文件很消耗系统内存,可能发生内存溢出或宕机,目前的解决方案就是调用系统命令来批量拷贝
java code
public static void copy(String orPath,String newPath)
{
String osName = System.getProperty("os.name");
//linu系统
if(Pattern.matches("Linux.*",osName))
{
//调用shell脚本
Runtime runtime = Runtime.getRuntime();
//copy.sh中 $1==/home/wasadmin/test1/aaa.doc $2==/home/wasadmin/test2
//String[] url = {"/bin/sh","-c","/home/wasadmin/copy.sh /home/wasadmin/test1/aaa.doc /home/wasadmin/test2"};
//由于用空格来区分参数,有的文件名中带有空格,所以用*通配符替换路径中的空格
String[] url = {"/bin/sh","-c","/home/wasadmin/copy.sh "
+(new File(orPath).getPath().replace(" ","*")) +" "+(new File(newPath).getPath())};
try
{
Process process = runtime.exec(url);
process.waitFor();
}catch(Exception e)
{
e.printStackTrace();
System.out.println("copy失败!");
}
}else if(Pattern.matches("Windows.*",osName))
{
//调用批处理文件
try
{
Runtime runtime = Runtime.getRuntime();
//copy.bat中 %1==d:\\test2\\aaa.doc %2==d:\\test3
//Process process = runtime.exec("cmd /c start d/copy.bat d:\\test2\\aaa.doc d:\\test3");
//由于用空格来区分参数,有的文件名中带有空格,所以用*通配符替换路径中的空格
Process process = runtime.exec("cmd /c start d/copy.bat "
+(new File(orPath).getPath().replace(" ","*")) +" "+(new File(newPath).getPath()));
process.waitFor();
}catch(Exception e)
{
e.printStackTrace();
System.out.println("copy失败!");
}
}
}
copy.bat
@echo off
:begin
xcopy /I /E /Y %1 %2
exit
copy.sh
cofile=$1
targetPath=$2
echo "cpFile=${cpFile}" > /home/wasadmin/log
echo "targetPath=${targetPath}" >> /home/wasadmin/log
if(test -e ${cpFile}); then
if(test -d ${targetPath}); then
echo "cp -R ${cpFile} ${targetPath}" >> /home/wasadmin/log
cp -R ${cpFile} ${targetPath}
result=$?
echo "result=${result}" >> /home/wasadmin/log
return $result;
else
echo "the target path is not exist" >> /home/wasadmin/log
return -2;
fi
else
echo "cop file is not exist" >> /home/wasadmin/log
return -1;
fi
java code
public static void copy(String orPath,String newPath)
{
String osName = System.getProperty("os.name");
//linu系统
if(Pattern.matches("Linux.*",osName))
{
//调用shell脚本
Runtime runtime = Runtime.getRuntime();
//copy.sh中 $1==/home/wasadmin/test1/aaa.doc $2==/home/wasadmin/test2
//String[] url = {"/bin/sh","-c","/home/wasadmin/copy.sh /home/wasadmin/test1/aaa.doc /home/wasadmin/test2"};
//由于用空格来区分参数,有的文件名中带有空格,所以用*通配符替换路径中的空格
String[] url = {"/bin/sh","-c","/home/wasadmin/copy.sh "
+(new File(orPath).getPath().replace(" ","*")) +" "+(new File(newPath).getPath())};
try
{
Process process = runtime.exec(url);
process.waitFor();
}catch(Exception e)
{
e.printStackTrace();
System.out.println("copy失败!");
}
}else if(Pattern.matches("Windows.*",osName))
{
//调用批处理文件
try
{
Runtime runtime = Runtime.getRuntime();
//copy.bat中 %1==d:\\test2\\aaa.doc %2==d:\\test3
//Process process = runtime.exec("cmd /c start d/copy.bat d:\\test2\\aaa.doc d:\\test3");
//由于用空格来区分参数,有的文件名中带有空格,所以用*通配符替换路径中的空格
Process process = runtime.exec("cmd /c start d/copy.bat "
+(new File(orPath).getPath().replace(" ","*")) +" "+(new File(newPath).getPath()));
process.waitFor();
}catch(Exception e)
{
e.printStackTrace();
System.out.println("copy失败!");
}
}
}
copy.bat
@echo off
:begin
xcopy /I /E /Y %1 %2
exit
copy.sh
cofile=$1
targetPath=$2
echo "cpFile=${cpFile}" > /home/wasadmin/log
echo "targetPath=${targetPath}" >> /home/wasadmin/log
if(test -e ${cpFile}); then
if(test -d ${targetPath}); then
echo "cp -R ${cpFile} ${targetPath}" >> /home/wasadmin/log
cp -R ${cpFile} ${targetPath}
result=$?
echo "result=${result}" >> /home/wasadmin/log
return $result;
else
echo "the target path is not exist" >> /home/wasadmin/log
return -2;
fi
else
echo "cop file is not exist" >> /home/wasadmin/log
return -1;
fi
相关文章推荐
- 在Windows、Linux下,分别利用BAT批处理文件和Shell脚本,生成类似“20150605_111327.txt”命名的文件
- linux下java调用shell和windows下java调用bat
- Java程序启动脚本(windows下的bat和linux下的sh)java applicat...
- windows到linux下面文本从ansi转utf-8(java调用shell命令,java文件复制)
- windows+java程序SSH登录linux调用shell脚本
- Linux系统中运行.sh(Shell脚本)文件
- 【Shell】 Linux下启动java程序的通用脚本sh
- Windows下bat脚本获取时间对比Linux下shell脚本获取时间
- java process调用可执行文件(windows 或者 linux)
- Java调用Windows批处理或Linux脚本或可执行文件
- Java调用批处理文件(.bat)
- Linux系统命令及Shell脚本学习笔记四:Linux文件系统
- Linux系统下高级语言调用shell脚本
- java JNI 调用c或c++,windows 或 Linux系统
- java调用pdfbox转pdf文件为图片文件,有中文时在windows上正常,部署到linux下乱码
- Java中执行Shell(.sh)和windows批量处理(.bat)
- 用shell脚本挂载linux主机拷贝相应文件copy.sh
- Windows写bat批处理文件执行java代码
- 如何在java程序中调用linux命令或者shell脚本
- java程序调用bat,sh文件