博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
面试题目两题写法
阅读量:7038 次
发布时间:2019-06-28

本文共 4206 字,大约阅读时间需要 14 分钟。

第一题:

题目打印:

面试题目两题写法

java写法:

public static void showTree(int level, File parentFolderPath) {        if (parentFolderPath.isDirectory()) {            File[] childFiles = parentFolderPath.listFiles();            for (File file : childFiles) {                showNameByLevel(level);                System.out.println(file.getName());                if (file.isDirectory()) {                    showTree(level + 1, file);                }            }        }    }    public static void showNameByLevel(int level) {        StringBuffer stringBuffer = new StringBuffer();        if (level > 0) {            for (int i = 0; i < level; i++) {                stringBuffer.append("\t");            }        }        if(stringBuffer.length() >0) System.out.print("|" + stringBuffer);        System.out.println("|");        if (stringBuffer.length()>0) System.out.print("|" + stringBuffer);        System.out.print("----");    }    @Test    public void c23() {        File file = new File("D:\\TOOL\\IDEASpace\\mavedome\\src\\test\\java");//        printlen(file, 0);        showTree(0, file);    }

python写法:

def fileCntIn(currPath):    '''''汇总当前目录下文件数'''    sum = 0    for root, dirs, files in os.walk(currPath, topdown=False):        for flie in files:            sum += len(flie)    return sum    # return sum([len(files) for root, dirs, files in os.walk(currPath,topdown=False)])def dirsTree(startPath):    '''''树形打印出目录结构'''    for root, dirs, files in os.walk(startPath):        # 获取当前目录下文件数        fileCount = fileCntIn(root)        # 获取当前目录相对输入目录的层级关系,整数类型        level = root.replace(startPath, '').count(os.sep)        # 树形结构显示关键语句        # 根据目录的层级关系,重复显示'| '间隔符,        # 第一层 '| '        # 第二层 '| | '        # 第三层 '| | | '        # 依此类推...        # 在每一层结束时,合并输出 '|____'        indent = '| ' * 1 * level + '|____'        print('%s%s -r:%s' % (indent, os.path.split(root)[1], fileCount))        for file in files:            indent = '| ' * 1 * (level + 1) + '|____'            print('%s%s' % (indent, file))if __name__ == '__main__':    dirsTree(os.getcwd())

第二题:

判断一些哪里有问题,如果有问题,怎么修改。

FileInputStream fileInputStream = null;        FileOutputStream fileOutputStream = null;        try {            fileInputStream = new FileInputStream("ttt");            fileOutputStream = new FileOutputStream("bbb");        }

修改为:

FileInputStream fileInputStream = null;        FileOutputStream fileOutputStream = null;        try {            fileInputStream = new FileInputStream("ttt");            fileOutputStream = new FileOutputStream("bbb");            int len = 0;            while ((len = fileInputStream.read()) != 0) {                fileOutputStream.write(len);            }        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally {            if (fileInputStream != null) {                try {                    fileInputStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (fileOutputStream != null) {                try {                    fileOutputStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }

写法:

public static void main(String[] args) {        FileInputStream fis = null;        FileOutputStream fos = null;        try {            fis = new FileInputStream("aaa.txt");            fos = new FileOutputStream("bbb.txt");            int b;            while ((b = fis.read()) != -1) {                fos.write(b);            }        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            if (fis != null) {                try {                    fis.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (fos != null) {                try {                    fos.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}

转载于:https://blog.51cto.com/357712148/2342053

你可能感兴趣的文章
设计模式学习笔记(十五:组合模式)
查看>>
继承与组合
查看>>
Spring 读取配置文件(一)
查看>>
转:JavaScript函数式编程(三)
查看>>
isnull的使用方法
查看>>
struts2和spring mvc的比较
查看>>
Apache shiro之权限校验流程
查看>>
变通实现微服务的per request以提高IO效率(二)
查看>>
css中px和em,rem
查看>>
Python“Non-ASCII character 'xe5' in file”报错问题(转)
查看>>
图解Spark API
查看>>
[Android Pro] AsyncTaskLoader vs AsyncTask
查看>>
Nodes “-1” are listed in ADOP_VALID_NODES table but not in FND_NODES table
查看>>
Java中将JSON格式的数据转换成对应的Bean、Map、List数据
查看>>
Linux下的tar压缩解压缩命令详解
查看>>
DevTools in Spring Boot 1.3
查看>>
Zabbix触发器支持的函数说明
查看>>
nginx听课随记杂记
查看>>
java-信息安全(四)-数据签名、数字证书
查看>>
bootstrap——bootstrap-table(1)
查看>>