zoukankan      html  css  js  c++  java
  • java作业 11.10

    package text3;
    
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Files;
    
    public class filesize {
    	
    double size=0.0;
    //计算文件或文件夹的大小,单位MB
    public double getSize(File file){
    	double n=file.length();
    //判断文件是否存在
    if(file.exists()) {
    if(!file.isFile()) {
    //获取文件大小
    File[] fl = file.listFiles();
    double ss=0;
    for(File f : fl)
    ss += getSize(f);
    return ss;
    }
    else {
    double ss = n/1024/1024;
    System.out.println(file.getName()+":"+ss+"MB");
    return ss;
    }
    }
    else {
    System.out.println("文件或文件夹不存在");
    return 0.0;
    }
    }
    public static void main(String[] args) throws IOException{
    filesize fd = new filesize();
    String path = "F:\xiaoshuo"; 
    File f = new File(path);
    double all = fd.getSize(f);
    System.out.println("All: "+all+"MB");
    }
    }
    

      

     

     

    package text3;
    
    import java.io.File;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    //编写一个文件加解密程序,通过命令行完成加解密工作
    public class filepass {
     private static final int numOfEncAndDec = 1;//加密解密密钥
     private static int dataOfFile=0;//文件字节内容
    
     public static void main(String[] args) {
      File srcFile=new File("F:\xiaoshuo\飘英文版.txt");//初始化文件
      File encFile=new File("F:\xiaoshuo\jiami.txt"); //加密文件
      File decFile=new File("F:\xiaoshuo\jiemi.txt");  //解密文件
      
      try {
        
          EncFile(srcFile,decFile);  //加密操作
    
          DecFile(decFile,encFile);
      }catch(Exception e) {
       e.printStackTrace();
      }
     }
     private static void EncFile(File srcFile,File encFile)throws Exception{
      if(!srcFile.exists()) {
       System.out.println("source file not exixt");
       }
      if(!encFile.exists()) {
       System.out.println("encrypt file created");
       encFile.createNewFile();//若无加密文件,新建一个加密文件
      }
      InputStream fis=new FileInputStream(srcFile);
      OutputStream fos=new FileOutputStream(encFile);
      
      while((dataOfFile=fis.read())>-1) {//当读到文件内容时
       fos.write(dataOfFile^numOfEncAndDec);//将读出的内容加密后写入
      }
      fis.close();
      fos.flush();
      fos.close();
     }
     private static void DecFile(File encFile,File decFile)throws Exception{
      if(!encFile.exists()) {
       System.out.println("加密文件不存在");
      }
      if(!decFile.exists()) {
       System.out.println("需要新建一个解密文件");
       decFile.createNewFile();
      }
      InputStream fis=new FileInputStream(encFile);
      OutputStream fos=new FileOutputStream(decFile);
      
      while((dataOfFile=fis.read())>-1) {
       fos.write(dataOfFile^numOfEncAndDec);
      }
      fis.close();
      fos.flush();
      fos.close();
     }
    
    }
    

      

    package text3;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class filefeng {
    public static void main(String[] args) {
    //调用cutFile()函数 传人参数分别为 (原大文件,切割后存放的小文件的路径,切割规定的内存大小)
    cutFile("F:\xiaoshuo\飘英文版.txt", "F:\xiaoshuo\cunfang.txt",1024 * 1024 * 20);
    }
    private static void cutFile(String yuan, String xin, int num) {
    FileInputStream fis = null;
    File file = null;
    try {
    fis = new FileInputStream(yuan);
    file = new File(yuan);
    //创建规定大小的byte数组
    byte[] c = new byte[num];
    int len = 0;
    //name为以后的小文件命名做准备
    int name = 1;
    //遍历将大文件读入byte数组中,当byte数组读满后写入对应的小文件中
    while ((len = fis.read(c)) != -1) {
    //分别找到原大文件的文件名和文件类型,为下面的小文件命名做准备
    String name2 = file.getName();
    int lastIndexOf = name2.lastIndexOf(".");
    String substring = name2.substring(0, lastIndexOf);
    String substring2 = name2.substring(lastIndexOf, name2.length());
    FileOutputStream fos = new FileOutputStream(xin + "\\"+ substring + "-" + name + substring2);
    //将byte数组写入对应的小文件中
    fos.write(c, 0, len);
    //结束资源
    fos.close();
    name++;
    }
    } 
    catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    

      

  • 相关阅读:
    springmvc @ResponseBody返回json 报406 not acceptable
    Java连接mysql中遇到的一些问题及解决方法
    nginx + keepalive 实现高可用
    nginx 内置变量
    nginx 防盗链
    nginx 跨域设置
    nginx 跨域设置
    nginx 日志分割
    servlet 下载地址 jcp
    图片压缩 jdk 1.8兼容问题
  • 原文地址:https://www.cnblogs.com/dwx8845/p/11829563.html
Copyright © 2011-2022 走看看