[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]java将16进制字符串写入文件,再读出的代码

上一篇:[备忘]Filed上注解,不能自定义一个组合注解?
下一篇:[备忘]写个bat自动恢复被暂停(挂起)的程序

添加日期:2024/9/4 10:23:59 快速返回   返回列表 阅读184次
不知道好使不。


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

}

 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved