服务器之家

服务器之家 > 正文

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

时间:2021-01-25 12:01     来源/作者:书思BookReflect

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

加密启动线程

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);
  }
}

解密启动线程

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);
  }
}

解密启动线程

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);
  }
}

文件对象序列化

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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;
  }
}

测试类

?
1
2
3
4
5
6
7
8
9
10
11
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文件的加密解密代码实例的全部内容,希望对大家有所帮助。有什么问题可以随时留言,小编会及时回复大家的。感谢朋友们对本站的支持!

原文链接:http://blog.csdn.net/qq_33624284/article/details/53607899

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
返回顶部