编程题:java编程实现在D盘根目录创建一个文件夹(homework),然后在该文件夹中创建两个文件夹(exercise1, exercise2),并在这二个文件夹中分别创建创建文件(file1.txt,file2.txt);运行效果如图所示。 d:homework创建成功。 d:homeworkexercise1创建成功。 d:homeworkexercise2创建成功。 d:homeworkexercise1ile1.txt创建成功。 d:homeworkexercise2ile2.txt创建成功 2,编程题:编程实现输出上一题建立的所有文件夹和文件的名称。运行效果如图所示 。 E:zzy jaua>jaua HomeWork2 d:Vhomework exerciseifile1.txt d:homework Vexercise1 ahomeworkexercise2file2.txt a= homework exercise2 E:/zzy jaua> 编程题:编程实现删除上一题中的所有文件和文件夹。!
时间: 2024-03-06 08:48:52 浏览: 60
第一个问题,以下是Java代码实现在D盘根目录创建一个文件夹(homework),然后在该文件夹中创建两个文件夹(exercise1, exercise2),并在这二个文件夹中分别创建创建文件(file1.txt,file2.txt):
```java
import java.io.File;
import java.io.IOException;
public class CreateFiles {
public static void main(String[] args) {
String path = "D:/homework";
File folder = new File(path);
if (!folder.exists()) {
boolean success = folder.mkdir();
if (success) {
System.out.println(path + "创建成功。");
} else {
System.out.println(path + "创建失败。");
return;
}
}
File subFolder1 = new File(path + "/exercise1");
if (!subFolder1.exists()) {
boolean success = subFolder1.mkdir();
if (success) {
System.out.println(path + "/exercise1创建成功。");
} else {
System.out.println(path + "/exercise1创建失败。");
return;
}
}
File subFolder2 = new File(path + "/exercise2");
if (!subFolder2.exists()) {
boolean success = subFolder2.mkdir();
if (success) {
System.out.println(path + "/exercise2创建成功。");
} else {
System.out.println(path + "/exercise2创建失败。");
return;
}
}
File file1 = new File(path + "/exercise1/file1.txt");
if (!file1.exists()) {
try {
boolean success = file1.createNewFile();
if (success) {
System.out.println(path + "/exercise1/file1.txt创建成功。");
} else {
System.out.println(path + "/exercise1/file1.txt创建失败。");
return;
}
} catch (IOException e) {
e.printStackTrace();
}
}
File file2 = new File(path + "/exercise2/file2.txt");
if (!file2.exists()) {
try {
boolean success = file2.createNewFile();
if (success) {
System.out.println(path + "/exercise2/file2.txt创建成功。");
} else {
System.out.println(path + "/exercise2/file2.txt创建失败。");
return;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
第二个问题,以下是Java代码实现输出上一题建立的所有文件夹和文件的名称:
```java
import java.io.File;
public class ListFiles {
public static void main(String[] args) {
String path = "D:/homework";
File folder = new File(path);
if (folder.exists() && folder.isDirectory()) {
System.out.println(path);
File[] subFolders = folder.listFiles();
for (File subFolder : subFolders) {
System.out.println("├──" + subFolder.getName());
if (subFolder.isDirectory()) {
File[] files = subFolder.listFiles();
for (File file : files) {
System.out.println("│ ├──" + file.getName());
}
}
}
} else {
System.out.println(path + "不存在或不是一个文件夹。");
}
}
}
```
第三个问题,以下是Java代码实现删除上一题中的所有文件和文件夹:
```java
import java.io.File;
public class DeleteFiles {
public static void main(String[] args) {
String path = "D:/homework";
File folder = new File(path);
if (folder.exists() && folder.isDirectory()) {
deleteFolder(folder);
System.out.println(path + "及其所有子文件和文件夹已被删除。");
} else {
System.out.println(path + "不存在或不是一个文件夹。");
}
}
private static void deleteFolder(File folder) {
if (folder.isDirectory()) {
File[] subFiles = folder.listFiles();
for (File subFile : subFiles) {
if (subFile.isDirectory()) {
deleteFolder(subFile);
} else {
subFile.delete();
}
}
}
folder.delete();
}
}
```
阅读全文