java读取minst数据集保存到本地磁盘(已实现),存入磁盘速度过慢,该如何优化?具体代码如下
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();
}
//黑底白字的图片
}
我想继续优化存入磁盘的操作,请问需要补充哪方面的知识,或者上面的代码该如何优化,请提供一下思路
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论