135-1821-9792

面试题目两题写法-创新互联

第一题:

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

创新互联致力于网站制作、成都网站建设,成都网站设计,集团网站建设等服务标准化,推过标准化降低中小企业的建站的成本,并持续提升建站的定制化服务水平进行质量交付,让企业网站从市场竞争中脱颖而出。 选择创新互联,就选择了安全、稳定、美观的网站建设服务!

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

    }
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻名称:面试题目两题写法-创新互联
分享URL:http://kswsj.com/article/dpdegj.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号