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);
    
      }
       
    }
  • 相关阅读:
    SET ROWCOUNT,SET NOCOUNT
    JS是按值传递还是按引用传递?
    Debug目录、Release目录,bin目录、obj目录,vshost.exe.config文件、.exe.config文件分析【C#】
    写window应用程序日志System.Diagnostics.EventLog.WriteEntry
    X-UA-Compatible设置兼容模式
    Linq的Distinct方法的扩展
    SQL Server 系统表简介
    sql server 常用的系统存储过程
    C# Timer用法及实例详解
    ASP.NET MVC内置的Filter实现介绍
  • 原文地址:https://www.cnblogs.com/ywqtro/p/11827692.html
Copyright © 2011-2022 走看看