您好,登錄后才能下訂單哦!
題目打印:
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):
'''''匯總當前目錄下文件數'''
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();
}
}
}
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。