題目打印:
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);
}
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)鍵語句
# 根據(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())
判斷一些哪里有問題,如果有問題,怎么修改。
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();
}
}
}
}
}
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。