不知道好使不。
package com.why;
import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List;
import org.apache.commons.io.FileUtils;
public class WriteFileTest {
public static void main(String[] args) throws IOException {
if (args.length < 1) { System.out.println("参数指定目录。"); return; } long start = System.currentTimeMillis(); System.out.println("----start----------"); System.out.println(args[0]); final String path = args[0]; List<Thread> tList = new ArrayList<>(); for(int i=0;i<50;i++) { Thread t = new Thread(new Runnable() {
@Override public void run() { System.out.println("----start----------"+Thread.currentThread().getName()); for(int loop=0;loop<1000;loop++) { System.out.println("----start----------"+loop+"-----"+Thread.currentThread().getName()); StringBuilder sb = new StringBuilder(); //sb.append("89504E470D0A1A0A0000000D46"); String hexStr = sb.toString(); //计算 int size = hexStr.length() / 2; byte[] bytes = new byte[size]; for (int i = 0; i < size; i++) { bytes[i] = Integer.valueOf(hexStr.substring(i * 2, i * 2 + 2), 16).byteValue(); } File target = new File(path+"/"+Thread.currentThread().getName()+"_"+loop+".jpg"); try { FileUtils.writeByteArrayToFile(target, bytes); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //读出来 String hex = null; try { hex = getFileHex(target); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } System.out.println(loop+":"+hexStr.equals(hex)); if(!hexStr.equals(hex)) { System.out.println("不相等"); try { Thread.sleep(300000000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } } } }); t.setName("t"+i); t.start(); tList.add(t); } for(Thread t:tList) { try { t.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } long end = System.currentTimeMillis(); System.out.println("----end----------"+(end-start)+"毫秒"); }
public static String getFileHex(File file) throws IOException { StringBuilder sb = new StringBuilder(); byte[] byteArray = FileUtils.readFileToByteArray(file); for (byte b : byteArray) { int v = b; if (v < 0) { v = v + 256; } String hex = Integer.toHexString(v).toUpperCase(); if (hex.length() == 1) { hex = "0" + hex; } sb.append(hex); } return sb.toString(); }
}
|