欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java探索之Thread+IO文件的加密解密代码实例

程序员文章站 2024-04-02 09:34:04
这篇文章向大家分享了几段代码,主要是关于thread+io文件的加密解密,下面看看具体代码: 加密启动线程 package com.hz.subsection;...

这篇文章向大家分享了几段代码,主要是关于thread+io文件的加密解密,下面看看具体代码:

加密启动线程

package com.hz.subsection;
import java.io.file;
public class encodefilethread extends thread {
  public files files;
  public file file;
  public file dst;
  public encodefilethread(string name,files files, file file,file dst) {
    super(name);
    this.dst = dst;
    this.files = files;
    this.file = file;
  }
  public void run() {
    files.encode(file,dst);
  }
}

解密启动线程

package com.hz.subsection;
import java.io.file;
public class encodefilethread extends thread {
  public files files;
  public file file;
  public file dst;
  public encodefilethread(string name,files files, file file,file dst) {
    super(name);
    this.dst = dst;
    this.files = files;
    this.file = file;
  }
  public void run() {
    files.encode(file,dst);
  }
}

解密启动线程

package com.hz.subsection;
import java.io.file;
public class decodefilethread extends thread {
  public files files;
  public file file;
  public file dst;
  public decodefilethread(string name,files files, file file,file dst) {
    super(name);
    this.dst = dst;
    this.files = files;
    this.file = file;
  }
  public void run() {
    files.decode(dst);
  }
}

文件对象序列化

package com.hz.subsection;
import java.io.bytearrayoutputstream;
import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.objectinputstream;
import java.io.objectoutputstream;
import java.io.serializable;
import java.util.arraylist;
import java.util.list;
public class files implements serializable {
  /**
   * 默认序列id
   */
  private static final long serialversionuid = 1l;
  private string filesno;
  private string name;
  private byte[] content;
  private boolean flag = true;
  public files() {
  }
  public files(string filesno){}
  public files(string filesno,string name, byte[] content) {
    super();
    this.name = name;
    this.content = content;
  }
  public string getfilesno() {
    return filesno;
  }
  public void setfilesno(string filesno) {
    this.filesno = filesno;
  }
  public string getname() {
    return name;
  }
  public void setname(string name) {
    this.name = name;
  }
  public byte[] getcontent() {
    return content;
  }
  public void setcontent(byte[] content) {
    this.content = content;
  }
  //加密序列化文件
  public synchronized void encode(file file,file dst) {
    if(!flag){
      try {
        wait();
      } catch (interruptedexception e) {
        e.printstacktrace();
      }
    }else{
      //获取文件夹下的每一个文件
      file[] chlidfiles = file.listfiles();
      list<files> list = new arraylist();
      for (int i = 0; i < chlidfiles.length; i++) {
        file tmpfile = chlidfiles[i];
        files files = getfiled(tmpfile);
        list.add(files);
      }
      savefiles(dst,list);
      flag = true;
      notifyall();
    }  
  }
  /**
   * 保存信息
   * @param dst
   * @param list
   */
  private void savefiles(file dst, list<files> list) {
    fileoutputstream fos = null;
    objectoutputstream oos = null;
    try {
      fos = new fileoutputstream(dst);
      oos = new objectoutputstream(fos);
      oos.writeobject(list);
    } catch (filenotfoundexception e) {
      e.printstacktrace();
    } catch (ioexception e) {
      e.printstacktrace();
    }finally{
      try {
        if(oos != null){
          oos.close();
          oos = null;
        }
        if(fos != null){
          fos.close();
          fos = null;
        }
      } catch (ioexception e) {
        e.printstacktrace();
      }
    }
  }
  public files getfiled(file tmpfile) {
    files files = new files();
    string name = tmpfile.getname();
    files.setname(name);
    fileinputstream fis = null;
    bytearrayoutputstream baos = null;
    try {
      fis = new fileinputstream(tmpfile);
      baos = new bytearrayoutputstream();
      byte[] buff = new byte[1024];
      int hasread = 0;
      while((hasread = fis.read(buff)) > -1){
        baos.write(buff, 0, hasread);
      }
      files.setcontent(baos.tobytearray());
      return files;
    } catch (filenotfoundexception e) {
      e.printstacktrace();
    } catch (ioexception e) {
      e.printstacktrace();
    }finally{
      try {
        if(baos != null){
          baos.close();
          baos = null;
        }
      } catch (ioexception e) {
        e.printstacktrace();
      }
      try {
        if(fis != null){
          fis.close();
          fis = null;
        }
      } catch (ioexception e) {
        e.printstacktrace();
      }
    }
    return null;
  }
  //解密序列化文件
  public synchronized void decode(file dst) {
    if(!flag){
      try {
        wait();
      } catch (interruptedexception e) {
        e.printstacktrace();
      }
    }else{
      list<files> list = readfiles(dst);
    for (files files : list) {
      string name = files.getname();
      byte[] content = files.getcontent();
      file file = new file(dst.getparent()+"//bbb",name);
      if(!file.exists()){
        try {
          file.createnewfile();
        } catch (ioexception e) {
          e.printstacktrace();
        }
      }
      fileoutputstream fos = null;
      try {
        fos = new fileoutputstream(file);
        fos.write(content);
      } catch (filenotfoundexception e) {
        e.printstacktrace();
      } catch (ioexception e) {
        e.printstacktrace();
      }finally{
        try {
          if(fos != null){
            fos.close();
            fos = null;
          }
          flag = false;
          notifyall();
        } catch (ioexception e) {
          e.printstacktrace();
        }
      }
    }
    }
  }
  private list<files> readfiles(file dst) {
    fileinputstream fis = null;
    objectinputstream ois = null;
    try {
      fis = new fileinputstream(dst);
      ois = new objectinputstream(fis);
      list<files> list = (list<files>) ois.readobject();
      return list;
    } catch (filenotfoundexception e) {
      e.printstacktrace();
    } catch (ioexception e) {
      e.printstacktrace();
    } catch (classnotfoundexception e) {
      e.printstacktrace();
    }finally{
      try {
        if(ois != null){
          ois.close();
          ois = null;
        }
      } catch (ioexception e) {
        e.printstacktrace();
      }
      try {
        if(fis != null){
          fis.close();
          fis = null;
        }
      } catch (ioexception e) {
        e.printstacktrace();
      }
    }
    return null;
  }
  public string tostring() {
    return "files [name="
        + name
        + ", content="
        + (content != null ? arraytostring(content, content.length)
            : null) + "]";
  }
  private string arraytostring(object array, int len) {
    stringbuffer buffer = new stringbuffer();
    buffer.append("[");
    for (int i = 0; i < len; i++) {
      if (i > 0)
        buffer.append(", ");
      if (array instanceof byte[])
        buffer.append(((byte[]) array)[i]);
    }
    buffer.append("]");
    return buffer.tostring();
  }
  public int hashcode() {
    return getfilesno().hashcode();
  }
  public boolean equals(object obj) {
    if(obj!=null && getclass() == files.class){
      files target = (files) obj;
      return target.getfilesno().equals(filesno);
    }
    return false;
  }
}

测试类

package com.hz.subsection;
import java.io.file;
public class testthread {
  public static void main(string[] args) {
    files files = new files("123");
    file file = new file("e:\\20160928javabase\\test\\aaa");
    file file2 = new file("e:\\20160928javabase\\test\\gg");
    new encodefilethread("加密文件", files,file ,new file(file, "dst.hz")).start();
    new decodefilethread("解密文件", files, file, new file(file, "dst.hz")).start();
  }
}

总结

以上就是本文关于java探索之thread+io文件的加密解密代码实例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:java创建与结束线程代码示例java多线程之线程通信生产者消费者模式及等待唤醒机制代码详解等,有什么问题可以随时留言,小编会及时回复大家的。感谢朋友们对本站的支持!