面试题目两题写法
admin
2023-07-26 03:40:10
0

第一题:

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

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

    }
}

相关内容

热门资讯

千万粉MCN放“黑料”敲诈明星... 6月29日,记者从广东公安获悉,近日,在公安部网安局和广东省公安厅网安总队的直接指挥下,河源市公安局...
辽宁葫芦岛一居民楼突发爆炸,已... 警情通报2026年6月29日7时11分,葫芦岛市消防救援局指挥中心接到报警,位于葫芦岛市南票区九龙街...
商都十二载,一轮焕新彰 郑州... 时光荏苒,初心如磐。为庆祝郑州绿地JW万豪酒店入驻中原12周年,酒店于城市之巅的JW花园隆重举办“夏...
做完数学题,韩国队回家了 随着刚果(金)逆转乌兹别克斯坦晋级美加墨世界杯淘汰赛,韩国球员心里绷了好几天的那根弦,“啪”的一下断...
军队、公安、司法、消防类院校2... 6月29日,河南省教育考试院公布河南省2026年军队院校、公安院校、司法类院校、中国消防救援学院的招...
国家体育总局政策法规司原副司长... 2026年6月29日,北京市第一中级人民法院依法公开宣判国家体育总局政策法规司原副司长胡光宇贪污案,...
鹤壁市人大常委会任免名单发布 鹤壁市人民代表大会常务委员会任免名单(2026年6月26日鹤壁市第十二届人民代表大会常务委员会第三十...
自由职业者也能评职称了?郑州出... 无论你是短剧演员还是服装设计师,无论你从事工程系列工作还是新型职业农民,只要是符合条件的“自由职业者...
河南省“社保通”服务平台上线 ... 河南日报讯 (全媒体记者 郭兵)想知道自己的养老金账户有多少钱?想快捷打印参保凭证?有社保疑问不知道...
国家体育总局原副司长胡光宇获刑... 2026年6月29日,北京市第一中级人民法院依法公开宣判国家体育总局政策法规司原副司长胡光宇贪污案,...