溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

面試題目?jī)深}寫法

發(fā)布時(shí)間:2020-04-03 17:02:09 來(lái)源:網(wǎng)絡(luò) 閱讀:260 作者:知止內(nèi)明 欄目:開發(fā)技術(shù)

第一題:

題目打?。?br/>面試題目?jī)深}寫法

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):
    '''''匯總當(dāng)前目錄下文件數(shù)'''
    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):
    '''''樹形打印出目錄結(jié)構(gòu)'''
    for root, dirs, files in os.walk(startPath):
        # 獲取當(dāng)前目錄下文件數(shù)
        fileCount = fileCntIn(root)
        # 獲取當(dāng)前目錄相對(duì)輸入目錄的層級(jí)關(guān)系,整數(shù)類型
        level = root.replace(startPath, '').count(os.sep)
        # 樹形結(jié)構(gòu)顯示關(guān)鍵語(yǔ)句
        # 根據(jù)目錄的層級(jí)關(guān)系,重復(fù)顯示'| '間隔符,
        # 第一層 '| '
        # 第二層 '| | '
        # 第三層 '| | | '
        # 依此類推...
        # 在每一層結(jié)束時(shí),合并輸出 '|____'
        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())

第二題:

判斷一些哪里有問(wèn)題,如果有問(wèn)題,怎么修改。

  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問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI