我的文件中有以ASCII码写的数字.例如,“ 9”被存储为两个字节57,即总共8位.
我想通过仅将这些数字存储为二进制值(例如从0-9的数字仅使用4位存储)来优化存储.
有帮助吗?
解决方法:
你可以这样写二进制文件
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Bin {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("\\test.bin");
String digits="12345";
char[] chars = digits.toCharArray();
for ( int i = 0 ; i < chars.length ; i+= 2 ) {
byte b1 = (byte) (chars[i] - (byte) '0');
byte b2 = (byte) (i < chars.length-1 ? chars[i+1] - (byte) '0' : 0xf);
fos.write((byte) ((b1 << 4) | b2 ));
}
fos.close();
FileInputStream fis = new FileInputStream("\\test.bin");
StringBuffer result = new StringBuffer();
byte[] buf = new byte[100];
int read = fis.read(buf);
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
for ( int i = 0 ; i < read ; i++ ) {
byte both = (byte) bais.read();
byte b1 = (byte) ((both >> 4 ) & 0xf);
byte b2 = (byte) (both & 0xf) ;
result.append( Character.forDigit(b1, 10));
if ( b2 != 0xf ) {
result.append(Character.forDigit(b2,10));
}
}
System.out.println(result.toString());
}
}
但我怀疑这会很有用
标签:store,file,ascii,java
来源: https://codeday.me/bug/20191202/2085408.html