zoukankan      html  css  js  c++  java
  • 文件与流课后作业

    package homework;
    //编写一个程序,指定一个文件夹,能自动计算出其总容量
    import java.io.File;
    import java.util.ArrayList;
    
    public class Test1 {
       static long size=0;
    private static ArrayList<String> filelist=new ArrayList<String>();
    public static void main(String[] args) {
      Test1 s=new Test1();
      String filePath="C:\\Users\\user\\Desktop\\over";
      s.getFiles(filePath);
      
    }
    
    void getFiles(String filePath) {
      
    File root=new File(filePath);
      File[] files=root.listFiles();
    package homework;
    
    import java.io.File;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    //编写一个文件加解密程序,通过命令行完成加解密工作
    public class Test2 {
        private static final int numOfEncAndDec = 0x99;// 加密解密密钥
        private static int dataOfFile = 0;// 文件字节内容
    
        public static void main(String[] args) {
            File srcFile = new File("C:\\Users\\user\\Desktop\\1.txt");
            File encFile = new File("C:\\Users\\user\\Desktop\\2.txt");  
            File decFile = new File("C:\\Users\\user\\Desktop\\.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("文件不存在");
            }
            if (!encFile.exists()) {
                System.out.println("新建文件");
                // 若无加密文件,新建一个加密文件
                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 homework;
    
    import java.io.File;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    //编写一个文件加解密程序,通过命令行完成加解密工作
    public class Test2 {
        private static final int numOfEncAndDec = 0x99;// 加密解密密钥
        private static int dataOfFile = 0;// 文件字节内容
    
        public static void main(String[] args) {
            File srcFile = new File("C:\\Users\\user\\Desktop\\1.txt");
            File encFile = new File("C:\\Users\\user\\Desktop\\2.txt");  
            File decFile = new File("C:\\Users\\user\\Desktop\\.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("文件不存在");
            }
            if (!encFile.exists()) {
                System.out.println("新建文件");
                // 若无加密文件,新建一个加密文件
                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();
        }
    
    }
    for(File file:files) {
      if(file.isDirectory()) {
          //public String getAbsolutePath()返回抽象路径名的绝对路径名字符串。
        getFiles(file.getAbsolutePath());
       filelist.add(file.getAbsolutePath());
       }else {
           //file.length():返回由此抽象路径名表示的文件的长度。
        size+=file.getAbsolutePath().length();
       }
      }
    System.out.println("大小是"+size);
    
      }
       
    }
  • 相关阅读:
    C# 反射设置属性帮助类
    WPF xaml中写代码
    redis 击穿、穿透、雪崩产生原因及解决方案
    Linux环境安装Tengine
    lsof使用说明
    Delve调试器简单使用说明
    web访问日志分析
    MongoDB中文社区 Freetalk,一起来玩快闪!
    在线研讨会:实时数据同步应用场景及实现方案探讨
    labview使用百度地图API,报错getContext方法未定义
  • 原文地址:https://www.cnblogs.com/ywqtro/p/11827692.html
Copyright © 2011-2022 走看看