undefinedfix
Sign in

Java reads MINST data set and saves it to local disk (implemented). The speed of saving it to disk is too slow. How to optimize it? The specific code is as follows

TLii edited in Fri, 23 Sep 2022

package com.microservice.zjh . tushare.ionio.data ;

import java . awt . Color ; import java . awt . image . BufferedImage ; import java . io . BufferedInputStream ; import java . io . File ; import java . io . FileInputStream ; import java . util . ArrayList ; import java . util . List ;

import javax.imageio.ImageIO ;

/**

  • chenjh

*/public class ReadMinst {

public static void main(String[] args) {
    // 1.读取文件
    File file = new File("src/main/java/com/microservice/zjh/tushare/ionio/data/minst/t10k-images.idx3-ubyte");

    try
    (
        //2.将文件注入文件输入流
        FileInputStream fis = new FileInputStream(file);
        //3.输入流注入到缓冲区
            //1)这是优点减少对磁盘的频率
        BufferedInputStream bis = new BufferedInputStream(fis);

    ) 
    {
        //4.设置缓冲区的大小
            //1)不需要在括号中写,小括号是写流的对象
        byte[] bytes = new byte[4];
        //5.读取到bytes中.从0,4 是输入 ,0,1,2,3
        bis.read(bytes, 0, 4);
        //6.bytes转hex[]
        //7.每个hex拼接Stringbuffer
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            byte b = bytes[i];
            //8.将转为无符号byte是int型,因为无符号byte区间是(0-255),所以在java中byte只能向上提升至int
            int unsignedbyte = b & 0xFF;
            //9.无符号byte转为String的hex(int转hex)
            String strHex = Integer.toHexString(unsignedbyte);
            //10.如果strHex长度为1,补0
            //11.拼接到StringBuffer,因为前四个值是32位
            if (strHex.length()<2) {
                sb.append(0);
            }
            sb.append(strHex);
        }
        System.out.println(sb);
        //11.StringBuffer转int,通过16进制字符串转为int
        int intfull = Integer.parseInt(sb.toString(),16);
        System.out.println(intfull);

        byte[] number_of_items = new byte[4];
        bis.read(number_of_items);
        StringBuffer number_of_items_sb = new StringBuffer();
        for (int i = 0; i < number_of_items.length; i++) {
            byte b = number_of_items[i];
            int unsignedbyte = b & 0xFF;
            String strHex = Integer.toHexString(unsignedbyte);
            if (strHex.length()<2) {
                number_of_items_sb.append(0);
            }
            number_of_items_sb.append(strHex);
        }
        System.out.println(number_of_items_sb);
        int number_of_items_int = Integer.parseInt(number_of_items_sb.toString(),16);
        System.out.println(number_of_items_int);
        bis.read(bytes, 0, 4);
        int xPixel = Integer.parseInt(bytesToHex(bytes), 16);           // 读取每行所含像素点数
        System.out.println(xPixel);
        bis.read(bytes, 0, 4);
        int yPixel = Integer.parseInt(bytesToHex(bytes), 16);   
        System.out.println(yPixel);
        //图片缓冲
        BufferedImage bi = new BufferedImage(28, 28, BufferedImage.TYPE_INT_BGR);
        for (int i = 0; i < number_of_items_int; i++) {
            // 1.保存图片到此地
            File saveinmnsit = new File("E:/mnsit/"+i+".png");
            if(saveinmnsit.exists()) {
                saveinmnsit.delete();
                saveinmnsit.createNewFile();
            }
            for (int y = 0; y < yPixel; y++) {
                for (int x = 0; x < xPixel; x++) {
                    int element = bis.read();
                    int rgb = new Color(element,element,element).getRGB(); 
                    if (element>127) {
                        System.out.print(1);
                    } else if(element<=127){
                        System.out.print(0);
                    }
                    bi.setRGB(x, y, rgb);
                }
                System.out.println();
            }
            //28*28跑完保存
            ImageIO.write(bi, "png", saveinmnsit);
            System.out.println();
            System.out.println();
            //执行几次,停止最外层运行for
            // if (i==0) {
            //     break;
            // }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } 
}

public static String bytesToHex(byte[] number_of_items) {
    StringBuffer number_of_items_sb = new StringBuffer();
    for (int i = 0; i < number_of_items.length; i++) {
        byte b = number_of_items[i];
        int unsignedbyte = b & 0xFF;
        String strHex = Integer.toHexString(unsignedbyte);
        if (strHex.length()<2) {
            number_of_items_sb.append(0);
        }
        number_of_items_sb.append(strHex);
    }
    return number_of_items_sb.toString();
}

//黑底白字的图片

}I want to continue to optimize the operation of saving to disk. What knowledge should I add or how to optimize the above code? Please provide some ideas

0 Replies