if(tempchars[i]=='\r'){
continue;
}else{
System.out.print(tempchars[i]);
}
}
}
}
}catch(Exceptione1){
e1.printStackTrace();
}finally{
if(reader!
=null){
try{
reader.close();
}catch(IOExceptione1){
}
}
}
}
/**
*以行为单位读取文件,常用于读面向行的格式化文件
*@paramfileName文件名
*/
publicstaticvoidreadFileByLines(StringfileName){
Filefile=newFile(fileName);
BufferedReaderreader=null;
try{
System.out.println("以行为单位读取文件内容,一次读一整行:
");
reader=newBufferedReader(newFileReader(file));
StringtempString=null;
intline=1;
//一次读入一行,直到读入null为文件结束
while((tempString=reader.readLine())!
=null){
//显示行号
System.out.println("line"+line+":
"+tempString);
line++;
}
reader.close();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(reader!
=null){
try{
reader.close();
}catch(IOExceptione1){
}
}
}
}
/**
*随机读取文件内容
*@paramfileName文件名
*/
publicstaticvoidreadFileByRandomAccess(StringfileName){
RandomAccessFilerandomFile=null;
try{
System.out.println("随机读取一段文件内容:
");
//打开一个随机访问文件流,按只读方式
randomFile=newRandomAccessFile(fileName,"r");
//文件长度,字节数
longfileLength=randomFile.length();
//读文件的起始位置
intbeginIndex=(fileLength>4)?
4:
0;
//将读文件的开始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[]bytes=newbyte[10];
intbyteread=0;
//一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
//将一次读取的字节数赋给byteread
while((byteread=randomFile.read(bytes))!
=-1){
System.out.write(bytes,0,byteread);
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(randomFile!
=null){
try{
randomFile.close();
}catch(IOExceptione1){
}
}
}
}
/**
*显示输入流中还剩的字节数
*@paramin
*/
privatestaticvoidshowAvailableBytes(InputStreamin){
try{
System.out.println("当前字节输入流中的字节数为:
"+in.available());
}catch(IOExceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args){
StringfileName="C:
/temp/newTemp.txt";
ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
ReadFromFile.readFileByLines(fileName);
ReadFromFile.readFileByRandomAccess(fileName);
}
}
二、将内容追加到文件尾部
importjava.io.FileWriter;
importjava.io.IOException;
importjava.io.RandomAccessFile;
/**
*将内容追加到文件尾部
*/
publicclassAppendToFile{
/**
*A方法追加文件:
使用RandomAccessFile
*@paramfileName文件名
*@paramcontent追加的内容
*/
publicstaticvoidappendMethodA(StringfileName,Stringcontent){
try{
//打开一个随机访问文件流,按读写方式
RandomAccessFilerandomFile=newRandomAccessFile(fileName,"rw");
//文件长度,字节数
longfileLength=randomFile.length();
//将写文件指针移到文件尾。
randomFile.seek(fileLength);
randomFile.writeBytes(content);
randomFile.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
/**
*B方法追加文件:
使用FileWriter
*@paramfileName
*@paramcontent
*/
publicstaticvoidappendMethodB(StringfileName,Stringcontent){
try{
//打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
FileWriterwriter=newFileWriter(fileName,true);
writer.write(content);
writer.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args){
StringfileName="C:
/temp/newTemp.txt";
Stringcontent="newappend!
";
//按方法A追加文件
AppendToFile.appendMethodA(fileName,content);
AppendToFile.appendMethodA(fileName,"appendend.\n");
//显示文件内容
ReadFromFile.readFileByLines(fileName);
//按方法B追加文件
AppendToFile.appendMethodB(fileName,content);
AppendToFile.appendMethodB(fileName,"appendend.\n");
//显示文件内容
ReadFromFile.readFileByLines(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)
{
System.out.println("新建目录操作出错");
e.printStackTrace();
}
}
/**
*新建文件
*/
publicvoidnewFile(StringfilePathAndName,StringfileContent)
{
try
{
StringfilePath=filePathAndName;
filePath=filePath.toString();
FilemyFilePath=newFile(filePath);
if(!
myFilePath.exists())
{
myFilePath.createNewFile();
}
FileWriterresultFile=newFileWriter(myFilePath);
PrintWritermyFile=newPrintWriter(resultFile);
StringstrContent=fileContent;
myFile.println(strContent);
resultFile.close();
System.out.println("新建文件操作成功执行");
}
catch(Exceptione)
{
System.out.println("新建目录操作出错");
e.printStackTrace();
}
}
/**
*删除文件
*/
publicvoiddelFile(StringfilePathAndName)
{
try
{
StringfilePath=filePathAndName;
filePath=filePath.toString();
FilemyDelFile=newFile(filePath);
myDelFile.delete();
System.out.println("删除文件操作成功执行");
}
catch(Exceptione)
{
System.out.println("删除文件操作出错");
e.printStackTrace();
}
}
/**
*删除文件夹
*/
publicvoiddelFolder(StringfolderPath)
{
try
{
delAllFile(folderPath);//删除完里面所有内容
StringfilePath=folderPath;
filePath=filePath.toString();
FilemyFilePath=newFile(filePath);
if(myFilePath.delete()){//删除空文件夹
System.out.println("删除文件夹"+folderPath+"操作成功执行");
}else{
System.out.println("删除文件夹"+folderPath+"操作执行失败");
}
}
catch(Exceptione)
{
System.out.println("删除文件夹操作出错");
e.printStackTrace();
}
}
/**
*删除文件夹里面的所有文件
*@parampathString文件夹路径如c:
/fqf
*/
publicvoiddelAllFile(Stringpath)
{
Filefile=newFile(path);
if(!
file.exists())
{
return;
}
if(!
file.isDirectory())
{
return;
}
String[]tempList=file.list();
Filetemp=null;
for(inti=0;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]);//再删除空文件夹
}
}
System.out.println("删除文件操作成功执行");
}
/**
*复制单个文件
*@paramoldPathString原文件路径如:
c:
/fqf.txt
*@paramnewPathString复制后路径如:
f:
/fqf.txt
*/
publicvoidcopyFile(StringoldPath,StringnewPath)
{
try
{
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();
}
System.out.println("删除文件夹操作成功执行");
}
catch(Exceptione)
{
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
/**
*复制整个文件夹内容
*@paramoldPathString原文件路径如:
c:
/