java操作_JAVA操作文件大全(一)
package com.pengyue;
import java.io.*;
public class FileOperate {
public FileOperate() {
}
/**
* 新建目錄
* @param folderPath String 如 c:/fqf
* @return boolean
*/
public void newFolder(String folderPath) {
try {
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
if (!myFilePath.exists()) {
myFilePath.mkdir();
}
}
catch (Exception e) {
System.out.println("新建目錄操作出錯");
e.printStackTrace();
}
}
/**
* 新建文件
* @param filePathAndName String 文件路徑及名稱 如c:/fqf.txt
* @param fileContent String 文件內容
* @return boolean
*/
public void newFile(String filePathAndName, String fileContent) {
try {
String filePath = filePathAndName;
filePath = filePath.toString();
File myFilePath = new File(filePath);
if (!myFilePath.exists()) {
myFilePath.createNewFile();
}
FileWriter resultFile = new FileWriter(myFilePath);
PrintWriter myFile = new PrintWriter(resultFile);
String strContent = fileContent;
myFile.println(strContent);
resultFile.close();
}
catch (Exception e) {
System.out.println("新建目錄操作出錯");
e.printStackTrace();
}
}
/**
* 刪除文件
* @param filePathAndName String 文件路徑及名稱 如c:/fqf.txt
* @param fileContent String
* @return boolean
*/
public void delFile(String filePathAndName) {
try {
String filePath = filePathAndName;
filePath = filePath.toString();
java.io.File myDelFile = new java.io.File(filePath);
myDelFile.delete();
}
catch (Exception e) {
System.out.println("刪除文件操作出錯");
e.printStackTrace();
}
}
/**
* 刪除文件夾
* @param filePathAndName String 文件夾路徑及名稱 如c:/fqf
* @param fileContent String
* @return boolean
*/
public void delFolder(String folderPath) {
try {
delAllFile(folderPath); //刪除完里面所有內容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); //刪除空文件夾
}
catch (Exception e) {
System.out.println("刪除文件夾操作出錯");
e.printStackTrace();
}
}
總結
以上是生活随笔為你收集整理的java操作_JAVA操作文件大全(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何查看一个组件的 classid是多少
- 下一篇: rubymine 保存成unix格式_如