首页 Java_IO操作_(读写、追加、删除、移动、复制、修改)

Java_IO操作_(读写、追加、删除、移动、复制、修改)

举报
开通vip

Java_IO操作_(读写、追加、删除、移动、复制、修改)------------------------------------------作者xxxx------------------------------------------日期xxxxJava_IO操作_(读写、追加、删除、移动、复制、修改)【精品文档】【精品文档】【精品文档】【精品文档】【精品文档】【精品文档】一、多种方式读文件内容。 1、按字节读取文件内容 2、按字符读取文件内容 3、按行读取文件内容 4、随机读取文件内容 Java代码  import ;  import ;  import ;  im...

Java_IO操作_(读写、追加、删除、移动、复制、修改)
------------------------------------------作者xxxx------------------------------------------日期xxxxJava_IO操作_(读写、追加、删除、移动、复制、修改)【精品文档】【精品文档】【精品文档】【精品文档】【精品文档】【精品文档】一、多种方式读文件内容。 1、按字节读取文件内容 2、按字符读取文件内容 3、按行读取文件内容 4、随机读取文件内容 Java代码  import ;  import ;  import ;  import ;  import ;  import ;  import ;  import ;  import ;   public class ReadFromFile {  /**    * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。    * @param fileName 文件的名    */  public static void readFileByBytes(String fileName){     File file = new File(fileName);     InputStream in = null;     try {      ("以字节为单位读取文件内容,一次读一个字节:");      // 一次读一个字节      in = new FileInputStream(file);      int tempbyte;      while((tempbyte=()) != -1){       (tempbyte);      }      ();     } catch (IOException e) {      ();      return;     }     try {      ("以字节为单位读取文件内容,一次读多个字节:");      //一次读多个字节      byte[] tempbytes = new byte[100];      int byteread = 0;      in = new FileInputStream(fileName);      (in);      //读入多个字节到字节数组中,byteread为一次读入的字节数      while ((byteread = (tempbytes)) != -1){       (tempbytes, 0, byteread);      }     } catch (Exception e1) {      e1.printStackTrace();     } finally {      if (in != null){       try {        ();       } catch (IOException e1) {       }      }     }  }  /**    * 以字符为单位读取文件,常用于读文本,数字等类型的文件    * @param fileName 文件名    */  public static void readFileByChars(String fileName){     File file = new File(fileName);     Reader reader = null;     try {      ("以字符为单位读取文件内容,一次读一个字节:");      // 一次读一个字符      reader = new InputStreamReader(new FileInputStream(file));      int tempchar;      while ((tempchar = ()) != -1){       //对于windows下,/r/n这两个字符在一起时,表示一个换行。       //但如果这两个字符分开显示时,会换两次行。       //因此,屏蔽掉/r,或者屏蔽/n。否则,将会多出很多空行。       if (((char)tempchar) != '/r'){        ((char)tempchar);       }      }      ();     } catch (Exception e) {      ();     }     try {      ("以字符为单位读取文件内容,一次读多个字节:");      //一次读多个字符      char[] tempchars = new char[30];      int charread = 0;      reader = new InputStreamReader(new FileInputStream(fileName));      //读入多个字符到字符数组中,charread为一次读取字符数      while ((charread = (tempchars))!=-1){       //同样屏蔽掉/r不显示       if1] != '/r')){        (tempchars);       }else{        for (int i=0; i 格式 pdf格式笔记格式下载页码格式下载公文格式下载简报格式下载 化文件    * @param fileName 文件名    */  public static void readFileByLines(String fileName){     File file = new File(fileName);     BufferedReader reader = null;     try {      ("以行为单位读取文件内容,一次读一整行:");      reader = new BufferedReader(new FileReader(file));      String tempString = null;      int line = 1;      //一次读入一行,直到读入null为文件结束      while ((tempString = ()) != null){       //显示行号       ("line " + line + ": " + tempString);       line++;      }      ();     } catch (IOException e) {      ();     } finally {      if (reader != null){       try {        ();       } catch (IOException e1) {       }      }     }  }  /**    * 随机读取文件内容    * @param fileName 文件名    */  public static void readFileByRandomAccess(String fileName){     RandomAccessFile randomFile = null;     try {      ("随机读取一段文件内容:");      // 打开一个随机访问文件流,按只读方式      randomFile = new RandomAccessFile(fileName, "r");      // 文件长度,字节数      long fileLength = ();      // 读文件的起始位置      int beginIndex = (fileLength > 4) ? 4 : 0;      //将读文件的开始位置移到beginIndex位置。      (beginIndex);      byte[] bytes = new byte[10];      int byteread = 0;      //一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。      //将一次读取的字节数赋给byteread      while ((byteread = (bytes)) != -1){       (bytes, 0, byteread);      }     } catch (IOException e){      ();     } finally {      if (randomFile != null){       try {        ();       } catch (IOException e1) {       }      }     }  }  /**    * 显示输入流中还剩的字节数    * @param in    */  private static void showAvailableBytes(InputStream in){     try {      ("当前字节输入流中的字节数为:" + ());     } catch (IOException e) {      ();     }  }    public static void main(String[] args) {     String fileName = "C:/temp/newTemp.txt";     (fileName);     (fileName);     (fileName);     (fileName);  }  }         二、将内容追加到文件尾部    import ;  import ;  import ;    /** * 将内容追加到文件尾部 */  public class AppendToFile {    /**    * A方法追加文件:使用RandomAccessFile    * @param fileName 文件名    * @param content 追加的内容    */  public static void appendMethodA(String fileName, String content){     try {      // 打开一个随机访问文件流,按读写方式      RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");      // 文件长度,字节数      long fileLength = ();      //将写文件指针移到文件尾。      (fileLength);      (content);      ();     } catch (IOException e){      ();     }  }  /**    * B方法追加文件:使用FileWriter    * @param fileName    * @param content    */  public static void appendMethodB(String fileName, String content){     try {      //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件      FileWriter writer = new FileWriter(fileName, true);      (content);      ();     } catch (IOException e) {      ();     }  }    public static void main(String[] args) {     String fileName = "C:/temp/newTemp.txt";     String content = "new append!";     //按方法A追加文件     (fileName, content);     (fileName, "append end. /n");     //显示文件内容     (fileName);     //按方法B追加文件     (fileName, content);     (fileName, "append end. /n");     //显示文件内容     (fileName);  }  }    三文件的各种操作类    import java.io.*;    /**  * 文件的各种操作 * @author 杨彩 * 文件操作 */    public class FileOperate  {    public FileOperate()  {  }  /** * 新建目录 */  public void newFolder(String folderPath)  {  try  {  String filePath = folderPath;  filePath = ();  File myFilePath = new File(filePath);  if(!())  {  ();  }  ("新建目录操作 成功执行");  }  catch(Exception e)  {  ("新建目录操作出错");  ();  }  }  /** * 新建文件 */  public void newFile(String filePathAndName, String fileContent)  {  try  {  String filePath = filePathAndName;  filePath = ();  File myFilePath = new File(filePath);  if (!())  {  ();  }  FileWriter resultFile = new FileWriter(myFilePath);  PrintWriter myFile = new PrintWriter(resultFile);  String strContent = fileContent;  (strContent);  ();  ("新建文件操作 成功执行");  }  catch (Exception e)  {  ("新建目录操作出错");  ();  }  }  /** * 删除文件 */  public void delFile(String filePathAndName)  {  try  {  String filePath = filePathAndName;  filePath = ();  File myDelFile = new File(filePath);  ();  ("删除文件操作 成功执行");  }  catch (Exception e)  {  ("删除文件操作出错");  ();  }  }  /** * 删除文件夹 */  public void delFolder(String folderPath)  {  try  {  delAllFile(folderPath); //删除完里面所有内容  String filePath = folderPath;  filePath = ();  File myFilePath = new File(filePath);  if(()) { //删除空文件夹  ("删除文件夹" + folderPath + "操作 成功执行");  } else {  ("删除文件夹" + folderPath + "操作 执行失败");  }  }  catch (Exception e)  {  ("删除文件夹操作出错");  ();  }  }  /** * 删除文件夹里面的所有文件 * @param path String 文件夹路径 如 c:/fqf */  public void delAllFile(String path)  {  File file = new File(path);  if(!())  {  return;  }  if(!())  {  return;  }  String[] tempList = ();  File temp = null;  for (int i = 0; i < ; i++)  {  if(())  {  temp = new File(path + tempList[i]);  }  else  {  temp = new File(path +  + tempList[i]);  }  if (())  {  ();  }  if (())  {  //delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件  delFolder(path+  + tempList[i]);//再删除空文件夹  }  }  ("删除文件操作 成功执行");  }  /** * 复制单个文件 * @param oldPath String 原文件路径 如: * @param newPath String 复制后路径 如: */  public void copyFile(String oldPath, String newPath)  {  try  {  int bytesum = 0;  int byteread = 0;  File oldfile = new File(oldPath);  if (())  {  //文件存在时  InputStream inStream = new FileInputStream(oldPath); //读入原文件  FileOutputStream fs = new FileOutputStream(newPath);  byte[] buffer = new byte[1444];  while ( (byteread = (buffer)) != -1)  {  bytesum += byteread; //字节数 文件大小  (bytesum);  (buffer, 0, byteread);  }  ();  }  ("删除文件夹操作 成功执行");  }  catch (Exception e)  {  ("复制单个文件操作出错");  ();  }  }  /** * 复制整个文件夹内容 * @param oldPath String 原文件路径 如:c:/fqf * @param newPath String 复制后路径 如:f:/fqf/ff */  public void copyFolder(String oldPath, String newPath)  {  try  {  (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹  File a=new File(oldPath);  String[] file=();  File temp=null;  for (int i = 0; i < ; i++)  {  if(())  {  temp=new File(oldPath+file[i]);  }  else  {  temp=new File(oldPath+File.separator+file[i]);  }  if(())  {  FileInputStream input = new FileInputStream(temp);  FileOutputStream output = new FileOutputStream(newPath + "/" +  (()).toString());  byte[] b = new byte[1024 * 5];  int len;  while ( (len = (b)) != -1)  {  (b, 0, len);  }  ();  ();  ();  }  if(())  {  //如果是子文件夹  copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);  }  }  ("复制文件夹操作 成功执行");  }  catch (Exception e)  {  ("复制整个文件夹内容操作出错");  ();  }  }  /** * 移动文件到指定目录 * @param oldPath String 如: * @param newPath String 如: */  public void moveFile(String oldPath, String newPath)  {  copyFile(oldPath, newPath);  delFile(oldPath);  }  /** * 移动文件到指定目录 * @param oldPath String 如: * @param newPath String 如: */  public void moveFolder(String oldPath, String newPath)  {  copyFolder(oldPath, newPath);  delFolder(oldPath);  }  public static void main(String args[])  {  String aa,bb;  boolean exitnow=false;  ("使用此功能请按[1] 功能一:新建目录");  ("使用此功能请按[2] 功能二:新建文件");  ("使用此功能请按[3] 功能三:删除文件");  ("使用此功能请按[4] 功能四:删除文件夹");  ("使用此功能请按[5] 功能五:删除文件夹里面的所有文件");  ("使用此功能请按[6] 功能六:复制文件");  ("使用此功能请按[7] 功能七:复制文件夹的所有内容");  ("使用此功能请按[8] 功能八:移动文件到指定目录");  ("使用此功能请按[9] 功能九:移动文件夹到指定目录");  ("使用此功能请按[10] 退出程序");  while(!exitnow)  {  FileOperate fo=new FileOperate();  try  {  BufferedReader Bin=new BufferedReader(new InputStreamReader(System.in));  String a=();  int b=(a);  switch(b)  {  case 1:System.out.println("你选择了功能一 请输入目录名");  aa=();  (aa);  break;  case 2:System.out.println("你选择了功能二 请输入文件名");  aa=();  ("请输入在"+aa+"中的内容");  bb=();  (aa,bb);  break;  case 3:System.out.println("你选择了功能三 请输入文件名");  aa=();  (aa);  break;  case 4:System.out.println("你选择了功能四 请输入文件名");  aa=();  (aa);  break;  case 5:System.out.println("你选择了功能五 请输入文件名");  aa=();  (aa);  break;  case 6:System.out.println("你选择了功能六 请输入文件名");  aa=();  ("请输入目标文件名");  bb=();  (aa,bb);  break;  case 7:System.out.println("你选择了功能七 请输入源文件名");  aa=();  ("请输入目标文件名");  bb=();  (aa,bb);  break;  case 8:System.out.println("你选择了功能八 请输入源文件名");  aa=();  ("请输入目标文件名");  bb=();  (aa,bb);  break;  case 9:System.out.println("你选择了功能九 请输入源文件名");  aa=();  ("请输入目标文件名");  bb=();  (aa,bb);  break;  case 10:exitnow=true;  ("程序结束,请退出");  break;  default("输入错误.请输入1-10之间的数");  }  ("请重新选择功能");  }  catch(Exception e)  {  ("输入错误字符或程序出错");  }  }  }  }  try{BufferedInputStreambis=newBufferedInputStream(newFileInputStream(file));byte[]buff=newbyte[(int)()];(buff);FileOutputStreamfos=newFileOutputStream(file);String[]lines=(newString(buff)).split("\n");for(Stringline:lines){(((sourceValue,formValue)+"\n").getBytes());}();();();}catch(FileNotFoundExceptionex){();}catch(IOExceptionioe){();}
本文档为【Java_IO操作_(读写、追加、删除、移动、复制、修改)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
个人认证用户
美丽的老师
暂无简介~
格式:doc
大小:174KB
软件:Word
页数:0
分类:
上传时间:2021-10-14
浏览量:0