public class DirStudy {
public static void main(String[] args) {
File file = new File("F:/EclipseWorkspace/Java300/temp/test");
//mkdir 父目录中又不存在的就创建失败 若已存在也创建失败
boolean flag = file.mkdir();
System.out.println(flag);
//mkdirs 会将文件链中不存在的目录都拿创建 推荐使用mkdirs
flag = file.mkdirs();
System.out.println(flag);
}
}
false
true
File file = new File("F:/EclipseWorkspace/Java300");
//返回该目录下的文件或目录的字符串名称 只返回一层
String[] subString = file.list();
for(String temp:subString) {
System.out.println(temp);
}
File[] subFile = file.listFiles();
for(File temp:subFile) {
System.out.println(temp.getPath());
}
.classpath
.project
.settings
bin
src
temp
F:\EclipseWorkspace\Java300\.classpath
F:\EclipseWorkspace\Java300\.project
F:\EclipseWorkspace\Java300\.settings
F:\EclipseWorkspace\Java300\bin
F:\EclipseWorkspace\Java300\src
F:\EclipseWorkspace\Java300\temp
递归打印目录下所有内容:
public static void printPath(File file, int deep) {
for (int i = 0; i < deep; i++) {
System.out.print("-");
}
System.out.println(file.getAbsolutePath());
if (file == null || !file.exists()) {
return;
} else if (file.isDirectory()) {
File[] temp = file.listFiles();
for (File t : temp) {
printPath(t, deep + 1);
}
}
}
输出:
F:\EclipseWorkspace\Java300
-F:\EclipseWorkspace\Java300\.classpath
-F:\EclipseWorkspace\Java300\.project
-F:\EclipseWorkspace\Java300\.settings
--F:\EclipseWorkspace\Java300\.settings\org.eclipse.jdt.core.prefs
-F:\EclipseWorkspace\Java300\bin
--F:\EclipseWorkspace\Java300\bin\ioStudy
---F:\EclipseWorkspace\Java300\bin\ioStudy\DirStudy.class
---F:\EclipseWorkspace\Java300\bin\ioStudy\IOstudy1.class
--F:\EclipseWorkspace\Java300\bin\test
---F:\EclipseWorkspace\Java300\bin\test\stringTest
----F:\EclipseWorkspace\Java300\bin\test\stringTest\ArrayCopy1.class
----F:\EclipseWorkspace\Java300\bin\test\stringTest\MyArrayList.class
----F:\EclipseWorkspace\Java300\bin\test\stringTest\MyHashMap.class
----F:\EclipseWorkspace\Java300\bin\test\stringTest\Node.class
----F:\EclipseWorkspace\Java300\bin\test\stringTest\TestBubbleSort.class
----F:\EclipseWorkspace\Java300\bin\test\stringTest\TestCollection.class
----F:\EclipseWorkspace\Java300\bin\test\stringTest\TestString.class
-F:\EclipseWorkspace\Java300\src
--F:\EclipseWorkspace\Java300\src\ioStudy
---F:\EclipseWorkspace\Java300\src\ioStudy\DirStudy.java
---F:\EclipseWorkspace\Java300\src\ioStudy\IOstudy1.java
--F:\EclipseWorkspace\Java300\src\test
---F:\EclipseWorkspace\Java300\src\test\stringTest
----F:\EclipseWorkspace\Java300\src\test\stringTest\ArrayCopy1.java
----F:\EclipseWorkspace\Java300\src\test\stringTest\MyArrayList.java
----F:\EclipseWorkspace\Java300\src\test\stringTest\MyHashMap.java
----F:\EclipseWorkspace\Java300\src\test\stringTest\TestBubbleSort.java
----F:\EclipseWorkspace\Java300\src\test\stringTest\TestCollection.java
----F:\EclipseWorkspace\Java300\src\test\stringTest\TestString.java
-F:\EclipseWorkspace\Java300\temp
--F:\EclipseWorkspace\Java300\temp\test
View Code
字节到字符是解码(decode),字符到字节是编码(encode)。