/**
* 创建文件夹目录
* @param pathName 文件夹路径
*/
public Boolean CreateFloder (String pathName) {
File myFolder = new File(pathName);
try {
if (!myFolder.exists()) {
myFolder.mkdirs(); //mkdir()只能创建一级目录,mkdirs()可以创建多级目录
}
return true;
} catch (Exception e) {
System.out.println("创建文件夹目录操作失败!");
e.printStackTrace();
return false;
}
}
/**
* 删除目录
* @param pathName
*/
public Boolean DelFolder (String pathName) {
File delFolderPath = new File(pathName);
try {
delFolderPath.delete(); //则此目录必须为空才能删除
return true;
} catch (Exception e) {
System.out.println("删除文件夹目录操作失败!");
e.printStackTrace();
return false;
}
}
/**
* 创建文件
* @param pathName 路径+文件名
* @param content 要写入文件的内容
*/
public Boolean CreateFile (String pathName,String content) {
File myFilePath = new File(pathName);
try {
if (!myFilePath.exists()) {
myFilePath.createNewFile();
}
FileWriter resultFile = new FileWriter(myFilePath);
PrintWriter myFile = new PrintWriter(resultFile);
myFile.println(content);
//myFile.write(content);
resultFile.close();
return true;
} catch (Exception e) {
System.out.println("创建文件操作失败");
e.printStackTrace();
return false;
}
}
/**
* 删除文件
* @param pathName 路径+文件名
*/
public Boolean DelFile (String pathName) {
File myDelFile = new File(pathName);
try {
myDelFile.delete();
return true;
} catch (Exception e) {
System.out.println("删除文件操作失败");
e.printStackTrace();
return false;
}
}
/**
* 删除该目录下的所有文件夹
* @param pathName
* @return
*/
public Boolean DelFileFromFolder (String pathName) {
File delFile = new File(pathName);
try {
File[] files = delFile.listFiles();
for (File file : files) {
if (file.isDirectory()){
file.delete();
}
}
return true;
} catch (Exception e) {
System.out.println("删除文件目录操作失败!");
e.printStackTrace();
return false;
}
}
/**
* 读取文件内容
* @param fileName
* @return
*/
public String ReadFile (File fileName) {
try {
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String content = new String();
String str = br.readLine();
while (str != null) {
System.out.println(str);
content += str;
str = br.readLine();
}
br.close();
fr.close();
return content;
} catch (FileNotFoundException e) {
e.printStackTrace();
return new String();
} catch (IOException e) {
e.printStackTrace();
return new String();
}
}
/**
* 写文件(删除原来的内容在写入)
* @param fileName
* @param content
* @return
*/
public Boolean WriteFile (File fileName,String content) {
try {
FileWriter fw = new FileWriter(fileName);
fw.write(content);
fw.flush();
fw.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}