代码中执行命令处理(面向Linux)

本文介绍了如何使用Java代码删除指定路径的文件及文件夹,并提供了详细的文件解压缩方法,包括处理目录和文件的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

删除文件:

                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)
        {
            //异常处理。
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值