java IO操作 读写追加删除移动复制等.docx

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

java IO操作 读写追加删除移动复制等.docx

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

java IO操作 读写追加删除移动复制等.docx

javaIO操作读写追加删除移动复制等

一、多种方式读文件内容。

1、按字节读取文件内容

2、按字符读取文件内容

3、按行读取文件内容

4、随机读取文件内容

importjava.io.BufferedReader;

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.FileReader;

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.InputStreamReader;

importjava.io.RandomAccessFile;

importjava.io.Reader;

publicclassReadFromFile{

/**

  *以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。

  *@paramfileName文件的名

  */

publicstaticvoidreadFileByBytes(StringfileName){

  Filefile=newFile(fileName);

  InputStreamin=null;

  try{

   System.out.println("以字节为单位读取文件内容,一次读一个字节:

");

   //一次读一个字节

   in=newFileInputStream(file);

   inttempbyte;

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

=-1){

    System.out.write(tempbyte);

   }

   in.close();

  }catch(IOExceptione){

   e.printStackTrace();

   return;

  }

  try{

   System.out.println("以字节为单位读取文件内容,一次读多个字节:

");

   //一次读多个字节

   byte[]tempbytes=newbyte[100];

   intbyteread=0;

   in=newFileInputStream(fileName);

   ReadFromFile.showAvailableBytes(in);

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

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

=-1){

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

   }

  }catch(Exceptione1){

   e1.printStackTrace();

  }finally{

   if(in!

=null){

    try{

     in.close();

    }catch(IOExceptione1){

    }

   }

  }

}

/**

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

  *@paramfileName文件名

  */

publicstaticvoidreadFileByChars(StringfileName){

  Filefile=newFile(fileName);

  Readerreader=null;

  try{

   System.out.println("以字符为单位读取文件内容,一次读一个字节:

");

   //一次读一个字符

   reader=newInputStreamReader(newFileInputStream(file));

   inttempchar;

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

=-1){

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

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

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

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

    if(((char)tempchar)!

='\r'){

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

    }

   }

   reader.close();

  }catch(Exceptione){

   e.printStackTrace();

  }

  try{

   System.out.println("以字符为单位读取文件内容,一次读多个字节:

");

   //一次读多个字符

   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]!

='\r')){

     System.out.print(tempchars);

    }else{

     for(inti=0;i

      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:

/

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

当前位置:首页 > 高等教育 > 农学

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

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