JAVA文件操作类和文件夹的操作Word下载.docx
《JAVA文件操作类和文件夹的操作Word下载.docx》由会员分享,可在线阅读,更多相关《JAVA文件操作类和文件夹的操作Word下载.docx(7页珍藏版)》请在冰豆网上搜索。
![JAVA文件操作类和文件夹的操作Word下载.docx](https://file1.bdocx.com/fileroot1/2022-10/27/0203f1d9-5d21-45bc-abf9-cc5f27eceab4/0203f1d9-5d21-45bc-abf9-cc5f27eceab41.gif)
class
FileOperate
{
private
String
message;
FileOperate()
}
/**
*
读取文本文件内容
@param
filePathAndName
带有完整绝对路径的文件名
encoding
文本文件打开的编码方式
@return
返回文本文件的内容
*/
readTxt(String
filePathAndName,String
encoding)
throws
IOException{
=
encoding.trim();
StringBuffer
str
new
StringBuffer("
"
);
st
;
try{
FileInputStream
fs
FileInputStream(filePathAndName);
InputStreamReader
isr;
if(encoding.equals("
)){
isr
InputStreamReader(fs);
}else{
InputStreamReader(fs,encoding);
}
BufferedReader
br
BufferedReader(isr);
data
while((data
br.readLine())!
=null){
str.append(data+"
}catch(Exception
e){
str.append(e.toString());
str.toString();
}catch(IOException
es){
return
st;
新建目录
folderPath
目录
返回目录创建后的路径
createFolder(String
folderPath)
txt
folderPath;
try
java.io.File
myFilePath
java.io.File(txt);
if
(!
myFilePath.exists())
myFilePath.mkdir();
catch
(Exception
e)
message
创建目录操作出错"
txt;
多级目录创建
准备要在本级目录下创建新目录的目录路径
例如
c:
myf
paths
无限级目录参数,各级目录以单数线区分
a|b|c
返回创建文件后的路径
myfac
createFolders(String
folderPath,
paths){
txts
StringTokenizer
StringTokenizer(paths,"
|"
for(int
i=0;
st.hasMoreTokens();
i++){
st.nextToken().trim();
if(txts.lastIndexOf("
/"
)!
=-1){
createFolder(txts+txt);
createFolder(txts+txt+"
创建目录操作出错!
txts;
新建文件
文本文件完整绝对路径及文件名
fileContent
文本文件内容
@return
void
createFile(String
filePathAndName,
fileContent)
filePath
filePathAndName;
filePath.toString();
File
File(filePath);
myFilePath.createNewFile();
FileWriter
resultFile
FileWriter(myFilePath);
PrintWriter
myFile
PrintWriter(resultFile);
strContent
fileContent;
myFile.println(strContent);
myFile.close();
resultFile.close();
创建文件操作出错"
有编码方式的文件创建
编码方式
GBK
或者
UTF-8
fileContent,
PrintWriter(myFilePath,encoding);
删除文件
Boolean
成功删除返回true遭遇异常返回false
boolean
delFile(String
filePathAndName)
bea
false;
myDelFile
if(myDelFile.exists()){
myDelFile.delete();
true;
(filePathAndName+"
删除文件操作出错"