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());
                    }
    
                }
            }
        }
  • 相关阅读:
    【若泽大数据实战第一天】大数据测试平台搭建
    数论的编程实验
    2016 百度之星资格赛 A题
    c++总结系列之期中版
    笔试面试算法题解之华为-成绩排序
    Linux光速入门之零基础安装Linux
    面试题
    SET QUOTED_IDENTIFIER
    环境变量
    eclipse的启动失败提示"发生了错误,请参阅日志文件"
  • 原文地址:https://www.cnblogs.com/csxf/p/3860214.html
Copyright © 2011-2022 走看看