zoukankan      html  css  js  c++  java
  • Java编写程序时要考虑到所有可能的异常

    要考虑到所有可能出现异常的情况,并对异常做处理操作,日志记录,不然程序会终止运行

    public void downFromCloud(String inputDir, String outputDir) {
            Configuration conf = new Configuration();
            // 实例化一个文件系统
            FileSystem fs = null;
            FSDataInputStream in = null;
            Path[] paths = null;
            OutputStream outs = null;
            try {
                fs = FileSystem.get(conf);
                Path inputPath = new Path(inputDir);
                FileStatus dirStatus = fs.getFileStatus(inputPath);
                if (!dirStatus.isDir()) {
                    return;
                }
                FileStatus[] filesStatus = fs.listStatus(inputPath,
                        new HadoopFileFilter(".*?\.xml"));
                paths = FileUtil.stat2Paths(filesStatus);
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                System.out.println("exception 1" + e1.getMessage());
                log4j.error("Download.class exception 1:"+e1.getMessage());
            }
            int length = paths.length;
            System.out.println("length:" + length);
            for (int i = 0; i < length; i++) {
                try {
    
                    in = fs.open(paths[i]);
                    // 将InputStrteam 中的内容通过IOUtils的copyBytes方法复制到OutToLOCAL中
                    outs = new FileOutputStream(new File(outputDir
                            + paths[i].getName()));
                    IOUtils.copyBytes(in, outs, 1024, true);
                    in.close();
                    outs.close();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    System.out.println("exception 2" + e.getMessage());
                    log4j.error("Download.class exception 2"+e.getMessage());
                } finally {
                    try {
                        in.close();
                        outs.close();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        System.out.println("exception 3" + e.getMessage());
                        log4j.error("Download.class exception 3:"+e.getMessage());
                    }
    
                }
            }
        }
  • 相关阅读:
    eclipse运行纯servlet程序
    VS中lib和dll
    eclipse部署web项目至本地的tomcat但在webapps中找不到
    【2018.07.28】(字符串/回文串)学习Manacher算法小记
    【2018.07.26】建立博客~
    【2018.07.27】(字符串/找相同)学习KMP算法小记
    [转]数据库范式那些事
    [转]Windows平台下的多线程编程
    mondrian schema学习(1)
    [转]SQL查询入门
  • 原文地址:https://www.cnblogs.com/csxf/p/3860214.html
Copyright © 2011-2022 走看看