JAVA课后作业

(一)

1.题目要求:

  编写一个程序,指定一个文件夹,能自动计算出其总容量。

2.源码:

  

package file;
import java.io.File;
public class Demo {
 double getsize(File file)//use recursion calculated the size of file
 {
  if(file.exists())//judege the file isn't exist
  {
   
   if(!file.isFile()) //judge the file is reall file or folder
   {
    File[] f1 = file.listFiles();
    double s=0;
    for(File f:f1)
    {
    s+=getsize(f); 
     
    }
    return s;
    
   }
   else
   {double s=(double)file.length()/1024/1024;
    
   System.out.println(file.getName()+":"+s+"MB");
    return s;
   }
  }
  else
  {
   System.out.println("文件不存在!");
   return 0.0;
  }
  }
 
 public static void main(String[] args) {
  Demo x=new Demo();
      double n=x.getsize(new File("D:\\腾讯"));
      System.out.println("All: "+n+"MB");
 }
 }
(二)
1.题目要求:
  编写一个文件加解密程序,通过命令行完成加解密工作.
2.源码:
  
package file_encryption;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Encryption {
 private static final int numOfEncAndDec=0x99;//加密解密密钥
 private static int dataOfFile=0;//文件字节内容
 public static void main(String[] args) {
  File srcFile=new File("E:\\新建文件夹\\text.txt");//初始化文件
  File encryptFile=new File("E:\\新建文件夹\\text1.txt"); //加密文件
  File decryptFile=new File("E:\\新建文件夹\\text2.txt");  //解密文件
 
  try {
  
   //EncFile(srcFile,encFile);  //加密操作方法
   //DecFile(encFile,decFile);//解密操作方法
   
      EncFile(srcFile,encryptFile);  //加密操作
      DecFile(encryptFile,decryptFile);
  }catch(Exception e) {
   e.printStackTrace();
  }
 }
 private static void EncFile(File srcFile,File encryptFile)throws Exception{
  if(!srcFile.exists()) {
   System.out.println("原文件不存在");
   }
  if(!encryptFile.exists()) {
   System.out.println("新建加密文件");
   encryptFile.createNewFile();//若无加密文件,新建一个加密文件
  }
  InputStream fis=new FileInputStream(srcFile);
  OutputStream fos=new FileOutputStream(encryptFile);
 
  while((dataOfFile=fis.read())>-1) {//当读到文件内容时
   fos.write(dataOfFile^numOfEncAndDec);//将读出的内容加密后写入
  }
  fis.close();
  fos.flush();
  fos.close();
 }
 private static void DecFile(File encryptFile,File decryptFile)throws Exception{
  if(!encryptFile.exists()) {
   System.out.println("加密文件不存在");
  }
  if(!decryptFile.exists()) {
   System.out.println("新建解密文件");
   decryptFile.createNewFile();
  }
  InputStream fis=new FileInputStream(encryptFile);
  OutputStream fos=new FileOutputStream(decryptFile);
 
  while((dataOfFile=fis.read())>-1) {
   fos.write(dataOfFile^numOfEncAndDec);
  }
  fis.close();
  fos.flush();
  fos.close();
 }
}
(三)
1.题目要求:
  编写一个文件分割工具,能把一个大文件分割成多个小的文件。并且能再次把它们合并起来得到完整的文件.
2.源码:
  
package divisionfile;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//编写一个文件分割工具,能把一个大文件分割成多个小的文件。并且能再次把它们合并起来得到完整的文件
public class Division {
    public static void main(String[] args) {
        //调用cutFile()函数 传人参数分别为 (原大文件,切割后存放的小文件的路径,切割规定的内存大小)
        cutFile("E:\\新建文件夹\\text2.txt", "E:\\新建文件夹",100);
    }
 private static void cutFile(String src, String endsrc, int num) {
        FileInputStream fis = null;
        File file = null;
        try {
            fis = new FileInputStream(src);
            file = new File(src);
            //创建规定大小的byte数组
            byte[] b = new byte[num];
            int len = 0;
            //name为以后的小文件命名做准备
            int name = 1;
            //遍历将大文件读入byte数组中,当byte数组读满后写入对应的小文件中
            while ((len = fis.read(b)) != -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(endsrc + "\\\\"+ substring + "-" + name + substring2);
                //将byte数组写入对应的小文件中
                fos.write(b, 0, len);
                //结束资源
                fos.close();
                name++;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    //结束资源
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

posted on 2019-11-06 14:26  LaughMe  阅读(222)  评论(0编辑  收藏  举报