• 文件与流课后作业


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

    package TEST1;
    import java.io.File;
    import java.util.ArrayList;
    public class total {
    static long Size=0;
    private static ArrayList<String>filelist=new ArrayList<String>();
    public static void main(String[] args)
    {
    total s =new total();
    String filePath="D:\QQ文件";
    s.getFile(filePath);
    }
    void getFile(String filePath)
    {
    File root=new File(filePath);
    File[]files=root.listFiles();
    for(File file:files)
    {
    if(file.isDirectory())
    {
    getFile(file.getAbsolutePath());
    filelist.add(file.getAbsolutePath());
    }
    else
    {
    Size+=file.getAbsolutePath().length();
    }
    }
    System.out.println("容量为:"+Size);
    }
    }

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

    package TEST1;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.security.Key;
    import java.security.SecureRandom;
    import javax.crypto.Cipher;
    import javax.crypto.CipherInputStream;
    import javax.crypto.CipherOutputStream;
    import javax.crypto.KeyGenerator;
    public class code {
    Key key;
    public code(String str)
    {
    getKey(str);
    }
    public void getKey(String strKey)
    {
    try
    {
    KeyGenerator _generator = KeyGenerator.getInstance("DES");

    _generator.init(new SecureRandom(strKey.getBytes()));

    this.key = _generator.generateKey();

    _generator = null;
    }
    catch (Exception e)
    {
    throw new RuntimeException("Error: " + e);
    }
    }
    public void encrypt(String file, String destFile) throws Exception
    {
    Cipher cipher = Cipher.getInstance("DES");
    cipher.init(Cipher.ENCRYPT_MODE, this.key);
    InputStream is = new FileInputStream(file);
    OutputStream out = new FileOutputStream(destFile);
    CipherInputStream cis = new CipherInputStream(is, cipher);
    byte[] buffer = new byte[1024];
    int r;
    while ((r = cis.read(buffer)) > 0) {
    out.write(buffer, 0, r);
    }
    cis.close();
    is.close();
    out.close();
    }
    public void decrypt(String file, String dest) throws Exception
    {
    Cipher cipher = Cipher.getInstance("DES");
    cipher.init(Cipher.DECRYPT_MODE, this.key);
    InputStream is = new FileInputStream(file);
    OutputStream out = new FileOutputStream(dest);
    CipherOutputStream cos = new CipherOutputStream(out, cipher);
    byte[] buffer = new byte[1024];
    int r;
    while ((r = is.read(buffer)) >= 0) {
    System.out.println();
    cos.write(buffer, 0, r);
    }
    cos.close();
    out.close();
    is.close();
    }
    public static void main(String[] args){
    code td = new code("aaa");
    try {
    td.encrypt("D:/r.txt", "D:/r加密后.txt");
    } catch (Exception e) {
    e.printStackTrace();
    }
    try {
    td.decrypt("D:/r加密后.txt", "D:/r解密后.txt");
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

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

    package TEST3;
    import java.io.File;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.InputStream;

    import java.io.OutputStream;
    public class pew {
    public static void splitFileDemo(File src, int m) throws IOException {

    if(src.isFile()) {

    //获取文件的总长度

    long l = src.length();

    //获取文件名

    String fileName = src.getName().substring(0, src.getName().indexOf("."));

    //获取文件后缀

    String endName = src.getName().substring(src.getName().lastIndexOf("."));

    System.out.println(endName);

    InputStream in = null;

    try {

    in = new FileInputStream(src);

    for(int i = 1; i <= m; i++) {

    StringBuffer sb = new StringBuffer();

    sb.append(src.getParent());

    sb.append("\");

    sb.append(fileName);

    sb.append("_data");

    sb.append(i);

    sb.append(endName);

    System.out.println(sb.toString());

    File file2 = new File(sb.toString());

    //创建写文件的输出流

    OutputStream out = new FileOutputStream(file2);

    int len = -1;

    byte[] bytes = new byte[10*1024*1024];

    while((len = in.read(bytes))!=-1) {

    out.write(bytes, 0, len);

    if(file2.length() > (l / m)) {

    break;

    }

    }

    out.close();

    }

    } catch (Exception e) {

    e.printStackTrace();

    } finally {

    if(in != null)

    in.close();

    }

    System.out.println("--- 文件分割完成 ---");

    }

    }

    public static void joinFileDemo(String[] src) {

    // 获取合并文件

    File newFile = new File(src[0].toString());

    // 获取文件名 后缀

    String fileName = newFile.getName().substring(0, newFile.getName().indexOf("_"));

    String endName = newFile.getName().substring(newFile.getName().lastIndexOf("."));

    // 得到新的文件名

    StringBuffer sb = new StringBuffer();

    sb.append(newFile.getParent());

    sb.append("\");

    sb.append(fileName);

    sb.append(endName);

    newFile = new File(sb.toString());

    for(int i = 0; i < src.length; i++) {

    File file = new File(src[i]);

    try {

    //读取小文件的输入流

    InputStream in = new FileInputStream(file);

    OutputStream out = new FileOutputStream(newFile, true);

    int len = -1;

    byte[] bytes = new byte[10*1024*1024];

    while((len = in.read(bytes))!=-1) {

    out.write(bytes, 0, len);

    }

    out.close();

    in.close();

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    System.out.println("文件合并完成!");

    }
    }

    package TEST3;

    import java.io.File;

    public class pew1 {
    public void main(String[] args) throws Exception {

    //分割文件

    pew.splitFileDemo(new File("TEST"), 2);

    //合并文件
    pew.joinFileDemo(new String[]{"TEST2", "TEST3"});
    }
    }

    实验结果截图传不上去。

  • 相关阅读:
    css修改element-ui滚动条样式
    执行java 报错 _System.out.printIn : 找不到符号
    java -version失效的解决方法
    vue cli4配置多个baseUrl环境,axios涉及多个请求域的情况
    vue cli4配置多个跨域_正则重复
    axios中vue 发送postman中raw_json格式的请求
    开发常用快捷键
    js字符串时间转化为时间戳
    vue项目vue-cli4展示本地markdown语法_md文件,图文详细讲解
    vue响应式原理源码:带你一步精通vue
  • 原文地址:https://www.cnblogs.com/jccjcc/p/9978735.html
Copyright © 2020-2023  润新知