⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filedes.java

📁 Java案例开发集锦,里面提供了很好的学习例子
💻 JAVA
字号:
//chp6
//FileDES.java

import java.io.*;
import java.nio.*;
import java.nio.channels.FileChannel;
public class FileDES{
        private static final boolean enc=true;    //加密
        private static final boolean dec=false;	  //解密
        private String sourceFileName;
        private String targetFileName;
        private String inKey;
        private boolean encdes;
        private File sourceFile;
        private File targetFile;
        private Des des;
        private void getpath(){
                String pathname;
                int pos=sourceFileName.lastIndexOf("/");
                pathname=sourceFileName.substring(0,pos);  //取得文件路径
                File dir=new File(pathname);
                if (!dir.exists()){       //判断是否存在
                        System.err.println(pathname+" is not exist");
                        System.exit(1);
                }else if(!dir.isDirectory()){     //判断是否为目录
                        System.err.println(pathname+" is not a directory");
                        System.exit(1);
                }

                pos=targetFileName.lastIndexOf("/");
                pathname=targetFileName.substring(0,pos);
                dir=new File(pathname);
                if (!dir.exists()){
                        if(!dir.mkdirs()){
                                System.out.println ("can not creat directory:"+pathname);
                                System.exit(1);
                        }
                }else if(!dir.isDirectory()){
                        System.err.println(pathname+" is not a directory");
                        System.exit(1);
                }
        }

        private static int bufcontent(FileChannel channel,ByteBuffer buf) throws IOException{
                long byteLeft=channel.size()-channel.position();
                if(byteLeft==0L)
                        return -1;
                buf.position(0);
                buf.limit(buf.position()+(byteLeft<8 ? (int)byteLeft :8));
                return channel.read(buf);
        }

        private void fileAction(boolean flag){
                des=new Des(inKey);
                FileOutputStream outputFile=null;
                try {
                        outputFile=new FileOutputStream(sourceFile,true);
            }catch (java.io.FileNotFoundException e) {
                e.printStackTrace(System.err);
            }
            FileChannel outChannel=outputFile.getChannel();

            try{
                    if(outChannel.size()%2!=0){
                                ByteBuffer bufTemp=ByteBuffer.allocate(1);
                                bufTemp.put((byte)32);
                                bufTemp.flip();
                                outChannel.position(outChannel.size());
                                outChannel.write(bufTemp);
                                bufTemp.clear();
                        }
                }catch(Exception ex){
                        ex.printStackTrace(System.err);
                        System.exit(1);
                }
                FileInputStream inFile=null;
                try{
                        inFile=new FileInputStream(sourceFile);
                }catch(java.io.FileNotFoundException e){
                        e.printStackTrace(System.err);
                }
                outputFile=null;
                try {
                        outputFile=new FileOutputStream(targetFile,true);
            }catch (java.io.FileNotFoundException e) {
                e.printStackTrace(System.err);
            }

            FileChannel inChannel=inFile.getChannel();
                outChannel=outputFile.getChannel();

                ByteBuffer inBuf=ByteBuffer.allocate(8);
                ByteBuffer outBuf=ByteBuffer.allocate(8);

                try{
                        String sourceStr;
                        String targetStr;
                        while(true){
                                if (bufcontent(inChannel,inBuf)==-1) break;
                                sourceStr=((ByteBuffer)(inBuf.flip())).asCharBuffer().toString();
                                inBuf.clear();
                                if (flag)     //若为true,则加密
                                        targetStr=des.enc(sourceStr,sourceStr.length());
                                else          //若为false,则解密
                                        targetStr=des.dec(sourceStr,sourceStr.length());
                            outBuf.clear();
                            if (targetStr.length()==4){
                                for (int i = 0; i<4; i++) {
                                        outBuf.putChar(targetStr.charAt(i));
                                }
                                outBuf.flip();
                            }else{
                                outBuf.position(0);
                                outBuf.limit(2*targetStr.length());
                                for (int i = 0; i<targetStr.length(); i++) {
                                        outBuf.putChar(targetStr.charAt(i));
                                }
                                outBuf.flip();
                            }

                            try {
                                outChannel.write(outBuf);
                                outBuf.clear();
                        }catch (java.io.IOException ex) {
                                ex.printStackTrace(System.err);
                        }
                        }
                        System.out.println (inChannel.size());
                        System.out.println (outChannel.size());
                        System.out.println ("EoF reached.");
                        inFile.close();
                        outputFile.close();
                }catch(java.io.IOException e){
                        e.printStackTrace(System.err);
                        System.exit(1);
                }
        }

        public FileDES(String sourceFileName,String targetFileName,String inKey,boolean encdes){
                this.sourceFileName=sourceFileName;
                this.targetFileName=targetFileName;
                this.encdes=encdes;
                getpath();
                sourceFile=new File(sourceFileName);
                targetFile=new File(targetFileName);
                this.inKey=inKey;
                if (encdes==enc)
                        fileAction(enc);  //执行加密操作
                else
                        fileAction(dec);  //执行解密操作
        }

        public static void  main(String[] args){
                String srcfile=System.getProperty("user.dir")+"/soucefile.doc"; //原始文件
                String cypfile=System.getProperty("user.dir")+"/cryptfile.doc"; //加密后的密文文件
                String trgfile=System.getProperty("user.dir")+"/targetfile.doc"; //解密后文件
                String passWord="ZYWX1234";            //工作密匙
                new FileDES(srcfile,cypfile,passWord,true);
                new FileDES(cypfile,trgfile,passWord,false);
        }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -