?? filedirectory.java
字號:
//FileDirectory.java
import java.io.*;
public class FileDirectory
{
public static void main(String[] args)
{
//如果沒有指定參數(shù),則缺省為當前目錄。
if (args.length == 0) args = new String[] { "." };
try
{
//新建指定目錄的File對象。
File currentPath = new File(args[0]);
//在指定目錄新建temp目錄的File對象。
File tempPath = new File(currentPath, "temp");
//用“tempPath”對象在指定目錄下創(chuàng)建temp目錄。
tempPath.mkdir();
//在temp目錄下創(chuàng)建兩個文件。
File temp1 = new File(tempPath, "temp1.txt");
temp1.createNewFile();
File temp2 = new File(tempPath, "temp2.txt");
temp2.createNewFile();
//遞歸顯示指定目錄的內(nèi)容。
System.out.println("顯示指定目錄的內(nèi)容");
listSubDir(currentPath);
//更改文件名“temp1.txt”為“temp.txt”。
File temp1new = new File(tempPath, "temp.txt");
temp1.renameTo(temp1new);
//遞歸顯示temp子目錄的內(nèi)容。
System.out.println("更改文件名后,顯示temp子目錄的內(nèi)容");
listSubDir(tempPath);
//刪除文件“temp2.txt”。
temp2.delete();
//遞歸顯示temp子目錄的內(nèi)容。
System.out.println("刪除文件后,顯示temp子目錄的內(nèi)容");
listSubDir(tempPath);
}
catch(IOException e)
{
System.err.println("IOException");
}
}
//遞歸顯示指定目錄的內(nèi)容。
static void listSubDir(File currentPath)
{
//取得指定目錄的內(nèi)容列表。
String[] fileNames = currentPath.list();
try
{
for (int i = 0; i < fileNames.length; i++)
{
File f = new File(currentPath.getPath(), fileNames[i]);
//如果是目錄,則顯示目錄名后,遞歸調(diào)用,顯示子目錄的內(nèi)容。
if (f.isDirectory())
{
//以規(guī)范的路徑格式顯示目錄。
System.out.println(f.getCanonicalPath());
//遞歸調(diào)用,顯示子目錄。
listSubDir(f);
}
//如果是文件,則顯示文件名,不包含路徑信息。
else System.out.println(f.getName());
}
}
catch(IOException e)
{
System.err.println("IOException");
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -