zoukankan      html  css  js  c++  java
  • 11.15java课后作业

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

    package Account;
    import java.io.File;
    import java.util.ArrayList;
    public class Size {
       static long size=0;
    private static ArrayList<String> filelist=new ArrayList<String>();
    public static void main(String[] args) {
         Size s=new Size();
    String filePath="E:\zxj.txt";//新建的文件夹zxj.txt
      s.getFiles(filePath);
    }
     void getFiles(String filePath) {
     File root=new File(filePath);
    
      File[] files=root.listFiles();
    
      for(File file:files) {
    
       if(file.isDirectory()) {
    
        getFiles(file.getAbsolutePath());
    
        filelist.add(file.getAbsolutePath());
    }
    
       else {
    
        size+=file.getAbsolutePath().length();
    
       }
    
      }
    
      System.out.println("大小是"+size);
    }
    }

    运行结果:

    2,编写一个文件加解密程序,通过命令行完成加解密工作

    package Account;
    import java.io.File;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    //编写一个文件加解密程序,通过命令行完成加解密工作
    public class FileCode {
     private static final int numOfEncAndDec=0x99;//加密解密密钥
     private static int dataOfFile=0;//文件字节内容
    
     public static void main(String[] args) {
      File srcFile=new File("E:\新建文件夹\poem.txt");//初始化文件
      File encFile=new File("E:\新建文件夹\poem1.txt"); //加密文件
      File decFile=new File("E:\新建文件夹\poem2.txt");  //解密文件
      
      try {
       //EncFile(srcFile,encFile);  //加密操作
       //DecFile(encFile,decFile);//解密操作
        
          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("encrypt file not exixt");
      }
      if(!decFile.exists()) {
       System.out.println("decrypt file created");
       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();
     }
    
    }

    3,编写一个文件分割工具,能把一个大文件分割成多个小的文件。并且能再次把它们合并起来得到完整的文件。

    package Account;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    public class FileSplit {
     private static int nameend = 1;//记录将要在旧名字上加上的编号以生成新文件
        public static void main(String[] args) {
            File ifile = new File("E:\zxj,txt");
            File ofile = new File("E:\zxj.txt");
            FileSplit fs = new FileSplit();
            fs.fileSplitMethod(ifile, ofile, 1024*1024*2);
        }    
        public boolean fileSplitMethod(File    ifile,File ofile,long filesize){
            boolean success = false;
            if(!ifile.exists() || !ifile.isFile() || !ofile.exists() || !ofile.isDirectory() ||  filesize <= 0)
                return success;
            int bufl = 1024; //缓冲字节数组的长度
            if(filesize < bufl)
                bufl = (int) filesize;
            byte[] buf = new byte[bufl];//字节缓冲数组
            int length = 0;//记录当前读取的字节数
            int size = 0;//记录当前文件字节数
            long readsize = 0;
            
            FileInputStream fis = null;
            FileOutputStream fos = null;
            
            try {
                fis = new FileInputStream(ifile);
                fos = new FileOutputStream(getNewFile(ifile,ofile));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                while((length = fis.read(buf)) != -1){
                    fos.write(buf,0,length);
                    size += length;
                    readsize += length;//记录总已经读取字节数
                    if((size + bufl) > filesize && readsize < ifile.length()){//如果再读一个数组会大于最大单个文件大小,并且还有未读字节,则创建新的fos。
                        fos.flush();//将缓冲中的写入文件
                        fos.close();//关闭这个输出流
                        fos = new FileOutputStream(getNewFile(ifile,ofile));//重新创建新的输出流
                        size = 0;
                    }
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return success;
        }    
        public File getNewFile(File    ifile,File ofile){
            File file = null;
            String oldname = ifile.getName();//得到旧名字,新名字将在旧名字上加上数字
            String newfilepath = ofile.getPath() + "\";//用于保存新的file路径
            int endIndex = oldname.lastIndexOf(".");
            if(endIndex > 0){
                String oldnameend = oldname.substring(endIndex, oldname.length());//保存扩展名
                oldname = oldname.substring(0, endIndex);//保存最后一个点左边的名字
                newfilepath += oldname + nameend++ + oldnameend;//新路径
            }else
                newfilepath += oldname + nameend++;
            file = new File(newfilepath);
            return file;
        }
    }

    ---恢复内容结束---

  • 相关阅读:
    9.16动手又动脑
    C#中集合的交集:Intersect问题
    LeetCode Easy: 27. Remove Element
    LeetCode Easy: 26.Remove Duplicates from Sorted Array
    LeetCode Easy: 21. Merge Two Sorted Lists
    LeetCode Easy: 20. Valid Parentheses
    LeetCode Easy: 14. Longest Common Prefix
    LeetCode Easy: 13. Roman to Integer
    LeetCode Easy: Palindrome Number
    DL: 初试 tensorflow
  • 原文地址:https://www.cnblogs.com/lover995/p/9985775.html
Copyright © 2011-2022 走看看