linux怎么查看本机内存大小
240
2022-11-15
Java——>字符型文件流,文件的常用操作方法
本文将文件的一些常用操作方法封装在了一个类中,欢迎交流学习
import java.io.*;public class FileOperate { //展示文件或文件夹 public void showFile(File file){ File[] files = file.listFiles(); if (files!= null && files.length != 0){ for(File f: files){ this.showFile(f); } } //做自己的显示,file是一个文件或者空文件夹 System.out.println(file.getAbsoluteFile()); } //删除文件或文件夹 public void deleteFile(File file){ File[] files = file.listFiles(); if (files!= null && files.length != 0){ for(File f: files){ this.deleteFile(f); } } //file是一个空文件夹或者文件时删除 file.delete(); } //遍历当前file的所有父目录 public void ergodic(File file){ File pFile = file.getParentFile(); while( pFile != null){ System.out.println(pFile.getAbsoluteFile()); pFile = pFile.getParentFile(); } //没有父则直接输出 System.out.println(file.getAbsoluteFile()); } //文件的复制 public void copyFile(File file, String path) throws IOException { FileInputStream fis = null; FileOutputStream fos = null; try { //根据传入的路径创建目标文件,getName方法获取该文件名称(没有路径),path传入的是路径 File newFile = new File(path+"//" + file.getName()); fis = new FileInputStream(file); fos = new FileOutputStream(newFile); byte[] b = new byte[1024]; int count = fis.read(b); while(count != -1){ fos.write(b,0,count); fos.flush(); //刷新缓冲区 count = fis.read(b); } System.out.println("复制完毕!!!"); } catch (IOException e) { e.printStackTrace(); } finally { //关闭 try { if(fis!=null) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(fos!=null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * //设计一个方法 文件的复制, 将file文件的内容全部复制到path路径处的文件 * 并且覆盖path原有的内容 * @param file * @param path */ public void copyFile(File file, String path){//当做file是一个文件 C://Test.txt BufferedInputStream bis = null; BufferedOutputStream bos = null; try { //创建一个输入流 bis = new BufferedInputStream(new FileInputStream(file)); //创建一个新的file对象 File newFile = new File(path+"//"+file.getName()); bos = new BufferedOutputStream(new FileOutputStream(newFile)); //读取文件中的信息 byte[] b = new byte[1024];//通常创建的数组 1kb--8kb之间 int count = bis.read(b);//1024 21有效的 while(count!=-1) { bos.write(b,0,count);//将读取到的有效字节 写入 bos.flush(); count = bis.read(b); } System.out.println("复制完毕"); } catch (IOException e) { e.printStackTrace(); } finally { //关闭 try { if(bis!=null) { bis.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(bos!=null) { bos.close(); } } catch (IOException e) { e.printStackTrace(); } } } public void encryptionFile(File file, String path){ FileInputStream fis = null; FileOutputStream fos = null; try { File newFile = new File(path + "//" + file.getName()); fis = new FileInputStream(file); fos = new FileOutputStream(newFile); byte[] b = new byte[1024]; int count = fis.read(b); while( count != -1){ //做点手脚 每一次数组的前两个元素位置互换 1024 byte temp = b[0]; b[0] = b[1]; b[1] = temp; fos.write(b,0,count); fos.flush(); count = fis.read(b); } System.out.println("加密完毕!!!"); } catch (IOException e) { e.printStackTrace(); } finally { //关闭 try{ if (fis != null){ fis.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (fos != null){ fos.close(); } } catch (IOException e) { e.printStackTrace(); } } } //文件夹的复制 file可以代表文件,也可以代表文件夹 public void superCopyFile(File file, String newPath){ //获取file的绝对路径 拼串的方式获取新文件的名字 String oldFilePath = file.getAbsolutePath(); String newFilePath = newPath + oldFilePath.split(":")[1]; File newFile = new File(newFilePath); //判断传入的文件是文件夹还是文件 File[] files = newFile.listFiles(); if (files != null ){ //如果file是文件夹,则创建文件夹 newFile.mkdir(); System.out.println("文件夹复制完毕!!!"); if (files.length != 0){ //再看文件夹里面是否有子元素,有子元素再递归操作子元素 for (File f: files){ this.encryptionFile(f, newPath); } } }else{ //如果穿进来的file是文件,则文件复制 FileInputStream fis = null; FileOutputStream fos = null; try{ fis = new FileInputStream(file); fos = new FileOutputStream(newFile); byte[] b = new byte[1024]; int count = fis.read(b); while (count != -1){ fos.write(b,0,count); fos.flush(); count = fis.read(b); } System.out.println("文件复制完毕!!!"); } catch (IOException e) { e.printStackTrace(); } finally { try{ if (fis != null){ fis.close(); } } catch (IOException e) { e.printStackTrace(); } try{ if (fos != null){ fos.close(); } } catch (IOException e) { e.printStackTrace(); } } } }}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~