java IO操作 读写追加删除移动复制等Word文档下载推荐.docx

上传人:b****7 文档编号:21959299 上传时间:2023-02-01 格式:DOCX 页数:13 大小:18.21KB
下载 相关 举报
java IO操作 读写追加删除移动复制等Word文档下载推荐.docx_第1页
第1页 / 共13页
java IO操作 读写追加删除移动复制等Word文档下载推荐.docx_第2页
第2页 / 共13页
java IO操作 读写追加删除移动复制等Word文档下载推荐.docx_第3页
第3页 / 共13页
java IO操作 读写追加删除移动复制等Word文档下载推荐.docx_第4页
第4页 / 共13页
java IO操作 读写追加删除移动复制等Word文档下载推荐.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

java IO操作 读写追加删除移动复制等Word文档下载推荐.docx

《java IO操作 读写追加删除移动复制等Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《java IO操作 读写追加删除移动复制等Word文档下载推荐.docx(13页珍藏版)》请在冰豆网上搜索。

java IO操作 读写追加删除移动复制等Word文档下载推荐.docx

);

//一次读一个字节

in=newFileInputStream(file);

inttempbyte;

while((tempbyte=in.read())!

=-1){

System.out.write(tempbyte);

}

in.close();

}catch(IOExceptione){

e.printStackTrace();

return;

以字节为单位读取文件内容,一次读多个字节:

//一次读多个字节

byte[]tempbytes=newbyte[100];

intbyteread=0;

in=newFileInputStream(fileName);

ReadFromFile.showAvailableBytes(in);

//读入多个字节到字节数组中,byteread为一次读入的字节数

while((byteread=in.read(tempbytes))!

System.out.write(tempbytes,0,byteread);

}catch(Exceptione1){

e1.printStackTrace();

}finally{

if(in!

=null){

}catch(IOExceptione1){

}

*以字符为单位读取文件,常用于读文本,数字等类型的文件

*@paramfileName文件名

publicstaticvoidreadFileByChars(StringfileName){

Readerreader=null;

以字符为单位读取文件内容,一次读一个字节:

//一次读一个字符

reader=newInputStreamReader(newFileInputStream(file));

inttempchar;

while((tempchar=reader.read())!

//对于windows下,\r\n这两个字符在一起时,表示一个换行。

//但如果这两个字符分开显示时,会换两次行。

//因此,屏蔽掉\r,或者屏蔽\n。

否则,将会多出很多空行。

if(((char)tempchar)!

='

\r'

){

System.out.print((char)tempchar);

reader.close();

}catch(Exceptione){

以字符为单位读取文件内容,一次读多个字节:

//一次读多个字符

char[]tempchars=newchar[30];

intcharread=0;

reader=newInputStreamReader(newFileInputStream(fileName));

//读入多个字符到字符数组中,charread为一次读取字符数

while((charread=reader.read(tempchars))!

=-1){

//同样屏蔽掉\r不显示

if((charread==tempchars.length)&

&

(tempchars[tempchars.length-1]!

)){

System.out.print(tempchars);

}else{

for(inti=0;

i<

charread;

i++){

if(tempchars[i]=='

continue;

System.out.print(tempchars[i]);

}finally{

if(reader!

*以行为单位读取文件,常用于读面向行的格式化文件

publicstaticvoidreadFileByLines(StringfileName){

BufferedReaderreader=null;

以行为单位读取文件内容,一次读一整行:

reader=newBufferedReader(newFileReader(file));

StringtempString=null;

intline=1;

//一次读入一行,直到读入null为文件结束

while((tempString=reader.readLine())!

//显示行号

line"

+line+"

:

"

+tempString);

line++;

*随机读取文件内容

publicstaticvoidreadFileByRandomAccess(StringfileName){

RandomAccessFilerandomFile=null;

随机读取一段文件内容:

//打开一个随机访问文件流,按只读方式

randomFile=newRandomAccessFile(fileName,"

r"

//文件长度,字节数

longfileLength=randomFile.length();

//读文件的起始位置

intbeginIndex=(fileLength>

4)?

4:

0;

//将读文件的开始位置移到beginIndex位置。

randomFile.seek(beginIndex);

byte[]bytes=newbyte[10];

//一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。

//将一次读取的字节数赋给byteread

while((byteread=randomFile.read(bytes))!

System.out.write(bytes,0,byteread);

}catch(IOExceptione){

if(randomFile!

randomFile.close();

*显示输入流中还剩的字节数

*@paramin

privatestaticvoidshowAvailableBytes(InputStreamin){

当前字节输入流中的字节数为:

+in.available());

publicstaticvoidmain(String[]args){

StringfileName="

C:

/temp/newTemp.txt"

;

ReadFromFile.readFileByBytes(fileName);

ReadFromFile.readFileByChars(fileName);

ReadFromFile.readFileByLines(fileName);

ReadFromFile.readFileByRandomAccess(fileName);

二、将内容追加到文件尾部

importjava.io.FileWriter;

*将内容追加到文件尾部

*/

publicclassAppendToFile{

*A方法追加文件:

使用RandomAccessFile

*@paramcontent追加的内容

publicstaticvoidappendMethodA(StringfileName,Stringcontent){

//打开一个随机访问文件流,按读写方式

RandomAccessFilerandomFile=newRandomAccessFile(fileName,"

rw"

//将写文件指针移到文件尾。

randomFile.seek(fileLength);

randomFile.writeBytes(content);

*B方法追加文件:

使用FileWriter

*@paramfileName

*@paramcontent

publicstaticvoidappendMethodB(StringfileName,Stringcontent){

//打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件

FileWriterwriter=newFileWriter(fileName,true);

writer.write(content);

writer.close();

Stringcontent="

newappend!

//按方法A追加文件

AppendToFile.appendMethodA(fileName,content);

AppendToFile.appendMethodA(fileName,"

appendend.\n"

//显示文件内容

//按方法B追加文件

AppendToFile.appendMethodB(fileName,content);

AppendToFile.appendMethodB(fileName,"

三文件的各种操作类

importjava.io.*;

*FileOperate.java

*文件的各种操作

*@author杨彩

*文件操作1.0

publicclassFileOperate

{

publicFileOperate()

*新建目录

publicvoidnewFolder(StringfolderPath)

try

StringfilePath=folderPath;

filePath=filePath.toString();

FilemyFilePath=newFile(filePath);

if(!

myFilePath.exists())

myFilePath.mkdir();

System.out.println("

新建目录操作成功执行"

catch(Exceptione)

新建目录操作出错"

e.printStackTrace();

*新建文件

publicvoidnewFile(StringfilePathAndName,StringfileContent)

StringfilePath=filePathAndName;

if(!

myFilePath.createNewFile();

FileWriterresultFile=newFileWriter(myFilePath);

PrintWritermyFile=newPrintWriter(resultFile);

StringstrContent=fileContent;

myFile.println(strContent);

resultFile.close();

新建文件操作成功执行"

catch(Exceptione)

*删除文件

publicvoiddelFile(StringfilePathAndName)

FilemyDelFile=newFile(filePath);

myDelFile.delete();

删除文件操作成功执行"

删除文件操作出错"

*删除文件夹

publicvoiddelFolder(StringfolderPath)

delAllFile(folderPath);

//删除完里面所有内容

if(myFilePath.delete()){//删除空文件夹

删除文件夹"

+folderPath+"

操作成功执行"

}else{

操作执行失败"

删除文件夹操作出错"

*删除文件夹里面的所有文件

*@parampathString文件夹路径如c:

/fqf

publicvoiddelAllFile(Stringpath)

Filefile=newFile(path);

file.exists())

return;

file.isDirectory())

String[]tempList=file.list();

Filetemp=null;

for(inti=0;

i<

tempList.length;

i++)

if(path.endsWith(File.separator))

temp=newFile(path+tempList[i]);

else

temp=newFile(path+File.separator+tempList[i]);

if(temp.isFile())

temp.delete();

if(temp.isDirectory())

//delAllFile(path+"

/"

+tempList[i]);

//先删除文件夹里面的文件

delFolder(path+File.separatorChar+tempList[i]);

//再删除空文件夹

*复制单个文件

*@paramoldPathString原文件路径如:

c:

/fqf.txt

*@paramnewPathString复制后路径如:

f:

publicvoidcopyFile(StringoldPath,StringnewPath)

intbytesum=0;

intbyteread=0;

Fileoldfile=newFile(oldPath);

if(oldfile.exists())

//文件存在时

InputStreaminStream=newFileInputStream(oldPath);

//读入原文件

FileOutputStreamfs=newFileOutputStream(newPath);

byte[]buffer=newbyte[1444];

while((byteread=inStream.read(buffer))!

=-1)

bytesum+=byteread;

//字节数文件大小

System.out.println(bytesum);

fs.write(buffer,0,byteread);

inStream.close();

删除文件夹操作成功执行"

复制单个文件操作出错"

*复制整个文件夹内容

/

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

当前位置:首页 > 经管营销 > 金融投资

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

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