//新建一个文件夹
public void newFolder(String folderPath) {
try {
String filePath = folderPath;
File myFilePath = new File(filePath);
if (!myFilePath.exists()) {
myFilePath.mkdir();
}
} catch (Exception e) {
System.out.println("新建文件夹操作出错");
e.printStackTrace();
}
}
//删除文件夹
public void delFolder(String folderPath){
try{
String filePath = folderPath;
File delPath = new File(filePath);
delPath.delete();
}catch (Exception e) {
System.out.println("删除文件夹操作出错");
e.printStackTrace();
}
}
//新建文件
public void createFile(String fileName){
try{
String myFileName = fileName;
if (!myFileName.exists()) {
myFileName.createNewFile();
}
}catch (Exception e) {
System.out.println("新建文件操作出错");
e.printStackTrace();
}
}
//删除文件
public void delFile(String fileName){
try{
String myFileName = fileName;
myFileName.delete();
}catch (Exception e) {
System.out.println("删除文件操作出错");
e.printStackTrace();
}
}
public void newFolder(String folderPath) {
try {
String filePath = folderPath;
File myFilePath = new File(filePath);
if (!myFilePath.exists()) {
myFilePath.mkdir();
}
} catch (Exception e) {
System.out.println("新建文件夹操作出错");
e.printStackTrace();
}
}
//删除文件夹
public void delFolder(String folderPath){
try{
String filePath = folderPath;
File delPath = new File(filePath);
delPath.delete();
}catch (Exception e) {
System.out.println("删除文件夹操作出错");
e.printStackTrace();
}
}
//新建文件
public void createFile(String fileName){
try{
String myFileName = fileName;
if (!myFileName.exists()) {
myFileName.createNewFile();
}
}catch (Exception e) {
System.out.println("新建文件操作出错");
e.printStackTrace();
}
}
//删除文件
public void delFile(String fileName){
try{
String myFileName = fileName;
myFileName.delete();
}catch (Exception e) {
System.out.println("删除文件操作出错");
e.printStackTrace();
}
}
本文介绍了使用Java进行文件和文件夹的基本操作,包括创建文件夹、删除文件夹、创建文件及删除文件的方法。通过示例代码展示了如何利用Java内置的File类来实现这些功能。
210

被折叠的 条评论
为什么被折叠?



