动手动脑

Posted 2205254761qq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动手动脑相关的知识,希望对你有一定的参考价值。

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

package 压缩包;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

public class Test extends SimpleFileVisitor<Path>{
    private static long size = 0;
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        File f = new File(file.toString());
        size += f.length();
        return FileVisitResult.CONTINUE;
    }
    public static void main(String[] args) {
        Path fileDirPath=Paths.get("D:/JAVA/LC");
        Test visitor=new Test();
        try {
            Files.walkFileTree(fileDirPath, visitor);
            System.out.println("文件夹"D:/JAVA/LC"总大小为" + size);
        }
        catch (IOException e) {
            e.printStackTrace();
        }    
    }
}

结果:

技术分享图片

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

package 压缩包;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class jiemi {
    public static void encryption() throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:/JAVA/LC/压缩包/1.txt"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:/JAVA/LC/压缩包/2.txt"));       
        int b;
        while((b = bis.read()) != -1) {
            bos.write(b ^ 123);
        }   
        bis.close();
        bos.close();
    }
    public static void main(String[] args) throws IOException {
        encryption();
    }
}

结果:

技术分享图片技术分享图片

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

 package 压缩包;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;

public class fenge extends SimpleFileVisitor<Path> {
    public static ArrayList<Path> smallfiles = new ArrayList<>();
    public static void main(String[] args) {
        cut("D:/JAVA/lc/压缩包/1.txt", "D:/JAVA/lc/压缩包/1", 1024 * 1024 * 1);
        paste("D:/JAVA/lc/压缩包/1", "D:/JAVA/lc/压缩包/", 1024 * 1024 * 1);
    }
    public static void cut(String filepath, String endpath, int sizeofsmallfile) {//原文件,切割后小文件路径,小文件最大容量
        FileInputStream fis = null;
        File file = null;
        try {
            fis = new FileInputStream(filepath);
            file = new File(filepath);
            byte[] b = new byte[sizeofsmallfile];
            int read = 0;
            int numberofsmallfile = 1;
            while ((read = fis.read(b)) != -1) {
                String nameoflargefile = file.getName();//获取大文件全名(带扩展名)
                String nameofsmallfile = nameoflargefile.substring(0, nameoflargefile.lastIndexOf("."));//获取小文件名(不带扩展名)
                String extension = nameoflargefile.substring(nameoflargefile.lastIndexOf("."), nameoflargefile.length());  //获取扩展名
                FileOutputStream fos = new FileOutputStream(endpath + "\\" + nameofsmallfile + "-" + numberofsmallfile + extension); //创建小文件
                fos.write(b, 0, read);//写入
                fos.close();
                numberofsmallfile++;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void paste(String smallfilespath, String endpath, int sizeofsmallfile) {//小文件路径,生成的大文件路径,小文件最大容量
        Path fileDirPath = Paths.get(smallfilespath);
        fenge visitor = new fenge();
        try {
            Files.walkFileTree(fileDirPath, visitor);
            File file = new File(smallfiles.get(0).toString());
            String nameofsmallfile = file.getName();//获取第一个小文件全名(带扩展名)
            String nameoflargefile = nameofsmallfile.substring(0, nameofsmallfile.lastIndexOf("-"));//获取大文件名(不带扩展名)
            String extension = nameofsmallfile.substring(nameofsmallfile.lastIndexOf("."), nameofsmallfile.length());//获取扩展名
            FileOutputStream fos = new FileOutputStream(endpath + "\\" + nameoflargefile + extension);//创建大文件
            byte[] b = new byte[sizeofsmallfile];
            for(Path p:smallfiles) {
                FileInputStream fis = new FileInputStream(p.toString());
                int read = fis.read(b);
                fis.close();
                fos.write(b, 0, read);//写入
            }
            fos.close();
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        if(attrs.isRegularFile()){
            smallfiles.add(file);
        }
        return FileVisitResult.CONTINUE;
    }
}







































































































































以上是关于动手动脑的主要内容,如果未能解决你的问题,请参考以下文章

动手动脑-4

动手动脑,无法自拔

方法的动手动脑

第三次实验及动手动脑

第八周动手动脑

动手动脑,第六次Tutorial——数组