11.15课后作业

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

import java.io.*;
public class Denglu 
{
public static void main(String[] args) throws IOException
{
try
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader inp=new BufferedReader(isr);//进行字节字符转换 用于键盘输入
String sdir="C:\\Users\\888888\\Desktop\\05.StreamAndFileSourceCode\\File";//文件
int count=0;//计算文件数目
File fdir1=new File(sdir);//创建文件夹
if(fdir1.exists()&&fdir1.isDirectory())
{
System.out.println(sdir+"存在");
String[] f1=fdir1.list();//列表 下面区分list()和listFile()
/*File f=new File("c:\\");
String[] f1=f.list();
File[] f2=f.listFiles();
① list() 返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录。 
以C盘为例,返回的是c盘下文件夹名字的字符串数组,如[TEMP, Windows]
②listFiles() 返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。
以C盘为例返回的是C盘下文件夹目录地址,如[c:\TEMP, c:\Windows]*/

for(int i=0;i<f1.length;i++)
{
System.out.println(f1[i]);//输出文件夹下的文件
count++;
}
System.out.println("一共有"+count+"文件");

//在目录下添加文件 名字jiahui
File fdir2=new File("C:\\\\Users\\\\888888\\\\Desktop\\\\05.StreamAndFileSourceCode\\\\File\\jiahui");
if(!fdir2.exists())
{
fdir2.mkdir();//如果不存在就创建 肯定不存在你指定的
}
count=0;
System.out.println("建立新的文件夹后: ");
for(int i=0;i<f1.length;i++)
{
System.out.println(f1[i]);//再一次输出目录下的问价及总数
count++;
}
System.out.println("一共有"+count+"文件");
}
System.out.println("请输入一个文件名字:");//对其中一个文件属性访问
String sfile=inp.readLine();
File ffile=new File(fdir1,sfile);//用一个已经存在代表某磁盘文件夹的fdir1对象作为文件夹 以sfile作为文件名字
if (ffile.isFile())
{
System.out.println(ffile.getName());
System.out.println(ffile.getPath());
System.out.println(ffile.length());
}

inp.close();
}
catch(IOException e)
{
System.out.println(e.toString());
}


}
}

 

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

 

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 TestDES {   

  Key key;   

  public TestDES(String str) {   

    getKey(str);//生成密匙   

  }   

  /**  

  * 根据参数生成KEY  

  */   

  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 initializing SqlMap class. Cause: " + e);   

    }   

  }   

  

  /**  

  * 文件file进行加密并保存目标文件destFile中  

  *  

  * @param file   要加密的文件 如c:/test/srcFile.txt  

  * @param destFile 加密后存放的文件名 如c:/加密后文件.txt  

  */   

  public void encrypt(String file, String destFile) throws Exception {   

    Cipher cipher = Cipher.getInstance("DES");   

    // cipher.init(Cipher.ENCRYPT_MODE, getKey());   

    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();   

  }   

  /**  

  * 文件采用DES算法解密文件  

  *  

  * @param file 已加密的文件 如c:/加密后文件.txt  

  *         * @param destFile  

  *         解密后存放的文件名 如c:/ test/解密后文件.txt  

  */   

  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){   

    TestDES td = new TestDES("aaa");   

    try {

td.encrypt("e:/r.txt", "e:/r加密后.txt");

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} //加密   

    try {

td.decrypt("e:/r加密后.txt", "e:/r解密后.txt");

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} //解密   

      

  }   

}

  

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

源代码

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 FileSliptUtil {

    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("文件合并完成!");

    }

    public class TestFileSlipt {

 

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

            //分割文件

            FileSliptUtil.splitFileDemo(new File("文件路径"), 2);

            //合并文件

            FileSliptUtil.joinFileDemo(new String[]{"文件路径", "文件路径"});

        }

    }

}

 

posted @ 2018-11-19 21:14  20173667  阅读(152)  评论(0编辑  收藏  举报