代码中执行命令处理(面向Linux)
2008-09-17 10:38
393 查看
删除文件:
String filePath = "";
String cmd = "rm -rf " + filePath;
Process process = Runtime.getRuntime().exec(cmd);
int cmdvalue = process.waitFor();//等子进程执行完即命令执行完毕返回才继续执行
if (cmdvalue == 0)
{
//处理
}
文件解压缩:
public static void unzipFile(String dir, String fileurl) throws Exception
{
int buffer = 1024;
File rootdir = new File(dir);
if (!rootdir.exists())
{
track.info(Helper.class, "unzipFile() create dir :" + rootdir,
log, Logger.DEBUG);
rootdir.mkdirs();
}
try
{
BufferedOutputStream dest = null;
FileInputStream fis = new FileInputStream(fileurl);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null)
{
track.info(Helper.class, "unzipFile() extracting: " + entry,
log, Logger.DEBUG);
int count;
byte data[] = new byte[buffer];
if (entry.isDirectory() || entry.getName().endsWith("//"))
{
// write the files to the disk
//是目录!
File subdir = null;
if (entry.getName().endsWith("//"))
{
track.info(Helper.class, "isdirectory! and endswith //!!!");
String entrynametemp = entry.getName().replaceAll("//",
"/");
subdir = new File(dir + entrynametemp);
} else
{
subdir = new File(dir + entry.getName());
}
if (!subdir.exists())
{
subdir.mkdirs();
track.info(Helper.class,
"unzipFile() create sub_dir:"
+ dir + entry.getName());
}
} else
{
//是文件!
FileOutputStream fos = new FileOutputStream(
dir + entry.getName());
track.info(Helper.class, "unzipFile() creating files : "
+ dir + entry.getName(), log, Logger.DEBUG);
dest = new BufferedOutputStream(fos, buffer);
while ((count = zis.read(data, 0, buffer)) != -1)
{
dest.write(data, 0, count);
}
dest.flush();
dest.close();
}
}
zis.close();
} catch (Exception ex)
{
//异常处理。
}
}
String filePath = "";
String cmd = "rm -rf " + filePath;
Process process = Runtime.getRuntime().exec(cmd);
int cmdvalue = process.waitFor();//等子进程执行完即命令执行完毕返回才继续执行
if (cmdvalue == 0)
{
//处理
}
文件解压缩:
public static void unzipFile(String dir, String fileurl) throws Exception
{
int buffer = 1024;
File rootdir = new File(dir);
if (!rootdir.exists())
{
track.info(Helper.class, "unzipFile() create dir :" + rootdir,
log, Logger.DEBUG);
rootdir.mkdirs();
}
try
{
BufferedOutputStream dest = null;
FileInputStream fis = new FileInputStream(fileurl);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null)
{
track.info(Helper.class, "unzipFile() extracting: " + entry,
log, Logger.DEBUG);
int count;
byte data[] = new byte[buffer];
if (entry.isDirectory() || entry.getName().endsWith("//"))
{
// write the files to the disk
//是目录!
File subdir = null;
if (entry.getName().endsWith("//"))
{
track.info(Helper.class, "isdirectory! and endswith //!!!");
String entrynametemp = entry.getName().replaceAll("//",
"/");
subdir = new File(dir + entrynametemp);
} else
{
subdir = new File(dir + entry.getName());
}
if (!subdir.exists())
{
subdir.mkdirs();
track.info(Helper.class,
"unzipFile() create sub_dir:"
+ dir + entry.getName());
}
} else
{
//是文件!
FileOutputStream fos = new FileOutputStream(
dir + entry.getName());
track.info(Helper.class, "unzipFile() creating files : "
+ dir + entry.getName(), log, Logger.DEBUG);
dest = new BufferedOutputStream(fos, buffer);
while ((count = zis.read(data, 0, buffer)) != -1)
{
dest.write(data, 0, count);
}
dest.flush();
dest.close();
}
}
zis.close();
} catch (Exception ex)
{
//异常处理。
}
}
相关文章推荐
- Java代码中执行Linux命令,亲测可用
- linux中执行命令权限不够怎样处理
- java程序执行linux命令,并线程阻塞,执行完才走下面代码
- Linux监控平台(主被动模式,添加监控主机,添加图形,处理图形乱码,远程执行命令)
- [linux学习笔记]第3天:变量分类,重定向,管道命令,程序执行流,文本处理类命令, 正则表达式,短路操作符
- java通过代码登录远程linux服务器并执行linux命令
- java中执行调用linux命令删除mysql数据库中的数据实例代码
- 纯Java通过SSH执行Linux命令的方法及代码
- 纯Java通过SSH执行Linux命令的方法及代码
- linux expect实现登陆远程主机并执行命令示例代码
- linux命令执行后的 返回值与错误代码
- 在代码中给文件设置权限(代码中执行linux命令)
- 在Linux操作系统上执行java代码,执行交互式命令
- android代码执行adb shell终端命令(linux命令)并返回执行结果
- 处理非完全root手机,在代码层批量执行有关联的命令
- Linux远程执行Shell命令或脚本
- Sed 与 Linux 等价命令代码[zt]
- 批量执行linux环境下的SCP命令
- ssh批量登录并执行命令的python实现代码
- linux常用命令一之文件处理命令