IO小应用

本文详细介绍了使用Java进行文件复制、加密与解密的方法,包括二进制文件的复制流程,基于密钥的文件加密与解密过程,以及字符集转换程序的实现。通过具体示例,展示了如何在Java中高效地处理文件操作与数据加密。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文件复制

import java.io.*;

public class FileCopy {
    public static void main(String[] args) throws IOException {
        String srcPath = "src.png";
        String destPath = "dest.png";

        // 1. 判断原文件
        File srcFile = new File(srcPath);
        if (!srcFile.exists()) {
            System.out.println("原文件不存在," + srcPath);
            return;
        }

        if (srcFile.isDirectory()) {
            System.out.println("原文件是目录," + srcPath);
            return;
        }

        // 2. 判断目标文件
        File destFile = new File(destPath);
        if (destFile.exists()) {
            System.out.println("目标文件已存在," + destPath);
            return;
        }

        // 3. 准备复制 - 以二进制形式复制
        // 一次读 4K 数据,把 4K 数据写入到目标文件中
        try (InputStream is = new FileInputStream(srcFile)) {
            try (OutputStream os = new FileOutputStream(destFile)) {
                byte[] buffer = new byte[4096];     // 4K 字节

                int len;
                while ((len = is.read(buffer, 0, 4096)) != -1) {
                    // buffer[0, len)
                    os.write(buffer, 0, len);
                }

                os.flush();
            }
        }
    }
}


文件加密解密

import java.io.*;

public class Encrypt {
    public static void main(String[] args) throws IOException {
        String secretKey = "Java14班";

        String srcPath = "原始情报文件.txt";
        String destPath = "加密后的情报文件.txt";

        try (InputStream is = new FileInputStream(srcPath)) {
            try (OutputStream os = new FileOutputStream(destPath)) {
                byte[] buffer = new byte[4096];
                int len;

                while ((len = is.read(buffer, 0, 4096)) != -1) {
                    encrypt(buffer, 0, len, secretKey);

                    os.write(buffer, 0, len);
                }

                os.flush();
            }
        }
    }

    private static byte calcSecretKey(String secretKey) throws IOException {
        byte k = 0;
        for (byte b : secretKey.getBytes("UTF-8")) {
            k += b;
        }

        return k;
    }

    private static void encrypt(byte[] buffer, int offset, int length, String secretKey) throws IOException {
        // 把每个 byte 都 + 一个值(从密钥计算得出)

        for (int i = 0; i < length; i++) {
            buffer[i + offset] = (byte)(buffer[i + offset] + calcSecretKey(secretKey));
        }
    }
}

import java.io.*;

public class Decrypt {
    public static void main(String[] args) throws IOException {
        String secretKey = "Java14班";
        String srcPath = "加密后的情报文件.txt";
        String destPath = "解密后的情报文件.txt";

        try (InputStream is = new FileInputStream(srcPath)) {
            try (OutputStream os = new FileOutputStream(destPath)) {
                byte[] buffer = new byte[4096];
                int len;

                while ((len = is.read(buffer, 0, 4096)) != -1) {
                    decrypt(buffer, 0, len, secretKey);

                    os.write(buffer, 0, len);
                }

                os.flush();
            }
        }
    }

    private static byte calcSecretKey(String secretKey) throws IOException {
        byte k = 0;
        for (byte b : secretKey.getBytes("UTF-8")) {
            k += b;
        }

        return k;
    }

    private static void decrypt(byte[] buffer, int offset, int length, String secretKey) throws IOException {
        // 把每个 byte 都 + 一个值(从密钥计算得出)

        for (int i = 0; i < length; i++) {
            buffer[i + offset] = (byte)(buffer[i + offset] - calcSecretKey(secretKey));
        }
    }
}

字符集转化程序

import java.io.*;

// 字符集转换的程序 —— iconv
public class IConv {
    public static void main(String[] args) throws IOException {
        if (args.length < 4) {
            System.out.println("请使用如下方式使用程序: ");
            System.out.println("IConv 源字符集 目标字符集 源文件 目标文件");
            return;
        }

        String fromCharset = args[0];
        String toCharset = args[1];
        String fromPath = args[2];
        String toPath = args[3];

        try (Reader reader = new InputStreamReader(new FileInputStream(fromPath), fromCharset)) {
            try (Writer writer = new OutputStreamWriter(new FileOutputStream(toPath), toCharset)) {
                char[] buffer = new char[4096];
                int len;

                while ((len = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, len);
                }

                writer.flush();
            }
        }
    }
}


复制所有程序,包括文件夹

import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CopyDirectory {
    private static class CopyFileTask implements Runnable {
        private final String srcPath;
        private final String destPath;

        CopyFileTask(String srcPath, String destPath) {
            this.srcPath = srcPath;
            this.destPath = destPath;
        }

        @Override
        public void run() {
            try {
                copyFile(srcPath, destPath);
                System.out.println(destPath + " 文件复制成功");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static String srcAbsPath;
    private static String desAbsPath;
    private static ExecutorService threadPool;

    public static void main(String[] args) throws IOException {
        /*
        if (args.length < 2) {
            System.out.println("请按照以下格式使用程序:");
            System.out.println("CopyDirectory 源目录 目标目录");
            return;
        }
        */

        threadPool = Executors.newFixedThreadPool(50);

        //String srcPath = "E:\\比特科技\\课程\\JavaWeb\\Java14班\\2020-04-11-IO";
        String srcPath = "E:\\videos";
        String destPath = "E:\\比特科技\\目标\\目标目录";
        //String srcPath = args[0];
        //String destPath = args[1];
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);

        srcAbsPath = srcFile.getAbsolutePath();
        desAbsPath = destFile.getAbsolutePath();

        // TODO: 检查源文件是否存在,是否是目录
        // TODO: 检查目标文件是否不存在
        // TODO: 检查目标文件的上一级目录是否存在

        copyDirectory(srcPath, destPath);

        threadPool.shutdown();
    }

    /**
     * 遍历所有的目录结构
     * 如果是目录,我也随着创建目录
     * 如果是文件,调用复制文件的方式进行复制
     * @param srcPath   源目录路径
     * @param destPath  目标目录路径
     * @throws IOException
     */
    private static void copyDirectory(String srcPath, String destPath) throws IOException {
        File srcRoot = new File(srcPath);
        travelDirectory(srcRoot);
    }

    private static void doDirectory(File directory) {
        // 为了找出要复制的目录的相对路径(相对于源路径 —— 根)
        String srcPath = directory.getAbsolutePath();
        String relativePath = srcPath.substring(srcAbsPath.length());
        String destPath = desAbsPath + relativePath;

        File destDirectory = new File(destPath);
        destDirectory.mkdir();  // 因为树从根往下遍历的,所以,上一级的目录,一定以及被创建了
        System.out.println(destPath + " 目录创建成功");
    }

    private static void doFile(File file) throws IOException {
        String srcPath = file.getAbsolutePath();
        String relativePath = srcPath.substring(srcAbsPath.length());
        String destPath = desAbsPath + relativePath;

        threadPool.execute(new CopyFileTask(srcPath, destPath));
    }

    // 这个是最慢的
    private static void copyFile(String srcPath, String destPath) throws IOException {
        try (InputStream is = new FileInputStream(srcPath)) {
            try (OutputStream os = new FileOutputStream(destPath)) {
                byte[] buffer = new byte[4096];
                int len;

                while ((len = is.read(buffer)) != -1) {
                    os.write(buffer, 0, len);
                }

                os.flush();
            }
        }
    }

    private static void doNode(File node) throws IOException {
        if (node.isDirectory()) {
            doDirectory(node);
        } else {
            doFile(node);
        }
    }

    private static void travelDirectory(File root) throws IOException {
        doNode(root);   // 被遍历的每个结点,都需要被调用

        if (root.isDirectory()) {
            File[] files = root.listFiles();
            if (files != null) {
                for (File file : files) {
                    travelDirectory(file);
                }
            }
        }
    }
}

资源下载链接为: https://pan.quark.cn/s/f989b9092fc5 在 Android 应用开发中,开发一款仿 OPPO 手机计算器的应用是极具实践价值的任务,它融合了 UI 设计、事件处理以及数学逻辑等多方面的技术要点。当前的“最新版仿 OPPO 手机计算器--android.rar”压缩包中,提供了该计算器应用的源代码,这为开发者深入学习 Android 编程提供了宝贵的资源。 UI 设计是构建此类计算器应用的基石。OPPO 手机的计算器界面以清晰的布局和良好的用户交互体验著称,其中包括数字键、运算符键以及用于显示结果的区域等关键元素。开发者需借助 Android Studio 中的 XML 布局文件来定义这些界面元素,可选用 LinearLayout、GridLayout 或 ConstraintLayout 等布局管理器,并搭配 Button 控件来实现各个按键功能。同时,还需考虑不同分辨率屏幕和设备尺寸的适配问题,这通常涉及 Density Independent Pixel(dp)单位的应用以及 Android 尺寸资源的合理配置。 事件处理构成了计算器的核心功能。开发者要在每个按钮的点击事件中编写相应的处理代码,通常通过实现 OnClickListener 接口来完成。例如,当用户点击数字键时,相应的值会被添加到显示区域;点击运算符键时,则会保存当前操作数并设定运算类型。而对于等号(=)按钮,需要执行计算操作,这往往需要借助栈数据结构来存储操作数和运算符,并运用算法解析表达式以完成计算。 数学逻辑的实现则是计算器功能的关键体现。在 Android 应用中,开发者可以利用 Java 内置的 Math 类,或者自行设计算法来完成计算任务。基本的加减乘除运算可通过简单的算术操作实现,而像求幂、开方等复杂运算则需调用 Math 类的相关方法。此外
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值