博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第九周动手动脑
阅读量:4578 次
发布时间:2019-06-09

本文共 3883 字,大约阅读时间需要 12 分钟。

1.文件分割工具(把大文件分割成多个小文件,并且能在把小文件合并成完整的文件)

package wenjianyuliu;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//编写一个文件分割工具,能把一个大文件分割成多个小的文件。并且能再次把它们合并起来得到完整的文件
public class CutFile {
    public static void main(String[] args) {
        //调用cutFile()函数 传人参数分别为 (原大文件,切割后存放的小文件的路径,切割规定的内存大小)
        cutFile("E:\\新建文件夹\\poem2.txt", "E:\\80747",1024 * 1024 * 20);
    }
    private static void cutFile(String src, String endsrc, int num) {
        FileInputStream fis = null;
        File file = null;
        try {
            fis = new FileInputStream(src);
            file = new File(src);
            //创建规定大小的byte数组
            byte[] b = new byte[num];
            int len = 0;
            //name为以后的小文件命名做准备
            int name = 1;
            //遍历将大文件读入byte数组中,当byte数组读满后写入对应的小文件中
            while ((len = fis.read(b)) != -1) {
                //分别找到原大文件的文件名和文件类型,为下面的小文件命名做准备
                String name2 = file.getName();
                int lastIndexOf = name2.lastIndexOf(".");
                String substring = name2.substring(0, lastIndexOf);
                String substring2 = name2.substring(lastIndexOf, name2.length());
                FileOutputStream fos = new FileOutputStream(endsrc + "\\\\"+ substring + "-" + name + substring2);
                //将byte数组写入对应的小文件中
                fos.write(b, 0, len);
                //结束资源
                fos.close();
                name++;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    //结束资源
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

 

2.文件加密程序(通过命令行完成加密解密工作)

package wenjianyuliu;import java.io.File;import java.io.InputStream;import java.io.OutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;//编写一个文件加解密程序,通过命令行完成加解密工作public class FileCode { private static final int numOfEncAndDec=0x99;//加密解密密钥 private static int dataOfFile=0;//文件字节内容 public static void main(String[] args) {  File srcFile=new File("E:\\新建文件夹\\poem.txt");//初始化文件  File encFile=new File("E:\\新建文件夹\\poem1.txt"); //加密文件  File decFile=new File("E:\\新建文件夹\\poem2.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("source file not exixt");   }  if(!encFile.exists()) {   System.out.println("encrypt file created");   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("encrypt file not exixt");  }  if(!decFile.exists()) {   System.out.println("decrypt file created");   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(); }} 3.指定一个文件,自动计算出总容量

package filetest;

import java.io.File;
import java.io.IOException;

public class FileEdit {

double size=0.0;
//计算文件或文件夹的大小,单位MB
public double getSize(File file){
//判断文件是否存在
if(file.exists()) {
if(!file.isFile()) {
//获取文件大小
File[] fl = file.listFiles();
double ss=0;
for(File f : fl)
ss += getSize(f);
return ss;
}else {
double ss = (double) file.length()/1024/1024;
System.out.println(file.getName()+":"+ss+"MB");
return ss;
}
}else {
System.out.println("文件或文件夹不存在,请检查文件路径是否正确!");
return 0.0;
}
}
public static void main(String[] args) throws IOException{
FileEdit fd = new FileEdit();
double all = fd.getSize(new File("C:\\Users\\FuHeishi826\\Desktop\\壹青年"));
System.out.println("All: "+all+"MB");
}
}

转载于:https://www.cnblogs.com/0518liu/p/9986800.html

你可能感兴趣的文章
java8中规范的四大函数式接口
查看>>
宝塔apache配置
查看>>
shell脚本中使用nohup执行命令不生效
查看>>
PHP 文件上传七牛云
查看>>
ZT:Unity与C++之间进行socket通信
查看>>
【转载】Maven入门实践
查看>>
【SQL Server备份恢复】提高SQL Server备份速度
查看>>
移位操作的疑问
查看>>
gitlab 邮件服务器配置
查看>>
Golang关键字—— if/else
查看>>
PHP&MySQL(三)——数组
查看>>
GPS.NET 和 GeoFramework开源了
查看>>
OFO和摩拜共享单车
查看>>
数据适配 DataAdapter对象
查看>>
有序列表ol和定义列表dl,dt,dd
查看>>
联想小新Air 15 安装黑苹果macOS High Sierra 10.13.6过程
查看>>
公共POI导出Excel方法–java
查看>>
次短路——Dijkstra
查看>>
Enter Query Mode Search Tricks Using Enter_Query Built-in in Oracle Forms
查看>>
广州夜景一
查看>>