温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

面试题目两题写法

发布时间:2020-04-03 17:02:09 来源:网络 阅读:232 作者:知止内明 栏目:开发技术

第一题:

题目打印:
面试题目两题写法

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();
                }
            }
        }

    }
}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI