JAVA文件操作类和文件夹的操作Word文件下载.docx
《JAVA文件操作类和文件夹的操作Word文件下载.docx》由会员分享,可在线阅读,更多相关《JAVA文件操作类和文件夹的操作Word文件下载.docx(7页珍藏版)》请在冰豆网上搜索。
文本文件打开的编码方式
@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
返回创建文件后的路径
myfa_c
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+"
删除文件操作出错"
e.toString();
bea;
删除文件夹
文件夹完整绝对路径
delFolder(String
delAllFile(folderPath);
//删除完里面所有内容
java.io.File(filePath);
myFilePath.delete();
//删除空文件夹
("
删除文件夹操作出错"
删除指定文件夹下所有文件
path
delAllFile(String
path)
file
File(path);
file.exists())
file.isDirectory())
String[]
tempList
file.list();
temp
null;
for
(int
i
0;
<
tempList.length;
i++)
(path.endsWith(File.separator))
File(path
+
tempList[i]);
File.separator
(temp.isFile())
temp.delete();
(temp.isDirectory())
delAllFile(path+"
//先删除文件夹里面的文件
delFolder(path+"
//再删除空文件夹
复制单个文件
oldPathFile
准备复制的文件源
newPathFile
拷贝到新绝对路径带文件名
copyFile(String
oldPathFile,
newPathFile)
int
bytesum
byteread
oldfile
File(oldPathFile);
(oldfile.exists())
{
//文件存在时
InputStream
inStream
FileInputStream(oldPathFile);
//读入原文件
FileOutputStream
FileOutputStream(newPathFile);
byte[]
buffer
byte[1444];
while((byteread
inStream.read(buffer))
!
-1){
+=
byteread;
//字节数
文件大小
System.out.println(bytesum);
fs.write(buffer,
0,
byteread);
inStream.close();
}catch
复制单个文件操作出错"
复制整个文件夹的内容
oldPath
准备拷贝的目录
newPath
指定绝对路径的新目录
copyFolder(String
oldPath,
newPath)
File(newPath).mkdirs();
//如果文件夹不存在
则建立新文件夹
a=new
File(oldPath);
file=a.list();
temp=null;
file.length;
if(oldPath.endsWith(File.separator)){
temp=new
File(oldPath+file[i]);
File(oldPath+File.separator+file[i]);
if(temp.isFile()){
input
FileInputStream(temp);
output
FileOutputStream(newPath
+
(temp.getName()).toString());
b
byte[1024
5];
len;
while
((len
input.read(b))
-1)
output.write(b,
len);
output.flush();
output.close();
input.close();
if(temp.isDirectory()){//如果是子文件夹
copyFolder(oldPath+"
+file[i],newPath+"
+file[i]);
复制整个文件夹内容操作出错"
移动文件
oldPath
newPath
moveFile(String
copyFile(oldPath,
newPath);
delFile(oldPath);
移动目录
moveFolder(String
copyFolder(oldPath,
delFolder(oldPath);
getMessage(){
this.message;