JAVA文件操作类和文件夹的操作Word文件下载.docx

上传人:b****6 文档编号:21318930 上传时间:2023-01-29 格式:DOCX 页数:7 大小:16.63KB
下载 相关 举报
JAVA文件操作类和文件夹的操作Word文件下载.docx_第1页
第1页 / 共7页
JAVA文件操作类和文件夹的操作Word文件下载.docx_第2页
第2页 / 共7页
JAVA文件操作类和文件夹的操作Word文件下载.docx_第3页
第3页 / 共7页
JAVA文件操作类和文件夹的操作Word文件下载.docx_第4页
第4页 / 共7页
JAVA文件操作类和文件夹的操作Word文件下载.docx_第5页
第5页 / 共7页
点击查看更多>>
下载资源
资源描述

JAVA文件操作类和文件夹的操作Word文件下载.docx

《JAVA文件操作类和文件夹的操作Word文件下载.docx》由会员分享,可在线阅读,更多相关《JAVA文件操作类和文件夹的操作Word文件下载.docx(7页珍藏版)》请在冰豆网上搜索。

JAVA文件操作类和文件夹的操作Word文件下载.docx

文本文件打开的编码方式

@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 

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());

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;

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 求职职场 > 社交礼仪

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1