Java. How to get constant read times from text file? - java

I have text file which contains over 1kk integer numbers. I want to read the n-th number in constant time. I'm not allowed to put all integers in the array. I heard that there is a technique which operates with bytes, so I could just write method "getNthInteger(int nth, int elementLengthInBytes)" or something like that. Please give me reference to this technique, any help is appreciated!

You convert each integer to an array of bytes of some length L, then write the bytes to the file. L must be exactly the same for each integer. Then to read integer N, read L bytes starting from byte N*L.
For example:
You can write an integer to a file as 4 bytes with java.io.RandomAccessFile.writeInt(int).
You can read the Nth integer with:
java.io.RandomAccessFile.seek(n*4);
int i = java.io.RandomAccessFile.readInt();
Replace java.io.RandomAccessFile with an actual object of type java.io.RandomAccessFile.

Related

java - Understanding byte array with delimeters

I'm trying to read AADHAAR QR code, a govt ID card in India. The user manual says -
Scanned data contains the following data fields in given sequence, which is embedded in byte array with the delimiter of byte value ”255” -
referenceId
name
date of birth
My question is,
What does it mean ?
Can someone explain with an example?
Decoding the Aadhaar QR code consists of several steps outlined in Unique Identification Authority of India (UIDAI) in chapter 3.2:
The first few steps are:
Convert the base10 value of Secure QR code into Big Integer.
Convert the Big Integer into byte array.
Decompress the byte array.
Read the value of byte array from index 0 to till first delimiter value "255" and convert this byte array value into string with encoding "ISO-8859-1". We will get the Email_mobile_present_bit_indicator_value as 0, 1, 2 or 3.
Once you have the decompressed array, you can simply search for the first element in the array containing -1, which is how the byte value 255 looks in Java as Java uses signed integer for bytes. The bytes before the -1 can be converted to a string.
These two steps are then repeating several times with the remaining data in the array to extract further values.
Extracting the photo and the signature are slightly different as they are not strings but binary data. Again, the steps are described in the referenced document.

Java - read method in FileInputStream - Why the numbers are different in read() and in read(byte[] b)?

read() method returns an int that represents the next byte of data and read(byte[] b) method does not return anything, it assigns the bytes data values to the array passed as an argument.
I have made some tests with an image file and I have taken 2 ways:
Print the results returned by read() method until this result is -1 (what means that the end of the file has been reached).
Create an array of bytes and pass it as an argument of read(byte[] b) method and print the numbers that have been assigned to that array of bytes.
I have noticed that the results in both cases are different: in the second case, as the results are of byte type, the numbers were not greater than 127 or less than -128; while in the first case, i found numbers greater than 200, for example.
Should not the numbers be the same in both cases due to the fact that the file is the same in both cases and those numbers represent the data of that file?
I also used a FileOutputStream to write the data of the file into another new file and in both cases, the new file had the same bytes and look the same (as I said, it was an image).
Thank you.
Since Java has only signed datatypes, read(byte[] b) reads regular bytes, i.e. -128-127. However read() returns an int so it can indicate end of stream with -1, returning unsigned byte values from 0-255.
byte b = (byte)in.read(); // Provided that stream has data left
Would give you an unsigned byte looking like the values you've gotten in your byte[] b.

Read file into binary byte array - Java

Is there any way to read a file into a byte array and have it built with binary numbers only? For both binary files and "regular" files (.txt etc.)(In Java)
I found a way to read a file into a byte array, but if the file is a binary file then the byte array contains negative numbers and i dont't know how to handle that as a binary number. I need my array to only contain 0s and 1s.
Even though there are negative values, the bits are still correct. The easiest thing you could do is wrap a BitSet around the byte[] so you can easily test individual bits:
BitSet bitSet = BitSet.valueOf(myByteArray);
boolean isBit20Set = bitSet.get(20);

how to find the type of integer from a file using java

I have a file that contains integer values of different bit lengths (4 bytes, 2 bytes), but I don't know the layout of these values in the file (i.e. whether a value is a 4 bytes or 2 bytes integer). For example, a file may have two 4-byte integers followed by five 2-byte integers, and another file may have three 2-byte integers first and then four 4-byte integers. Is there a way to read such values?
I want to write code that takes such a file and reads a value irrespective of its byte size. Right now I am using DataInputStream, and by knowing the layout of the values, using some viewer in advance to read the values. But in this manner everything is hard coded, and my code is not generic.
Your going to have to "parse" or "read" or "do something" with the viewer data and use the refactored viewer info as file format definition info during the reads.

How to convert numbers stored as two character ASCII strings to binary?

I have numbers written as ASCII codes each of 2 bytes which wastes a lot of the space. I want to convert those number to their corresponding ASCII code to save the space.
Any idea?
If you mean characters, Java uses two bytes per character as part of its Unicode support. If you give ASCII values, Java will make the upper byte zero. You won't save a thing.
If you mean floats or doubles or ints, the bytes per value are fixed there as well.
You're barking up the wrong tree. I don't think this will save you anything no matter what you do.
You're better off writing C or C++ if you need that kind of optimization, not Java.
My first thought is that this is an imagined optimization that isn't supported by data. The only application that would justify something like this would be scientific computing on a large scale. Even that wouldn't justify it, because you'll need more precision than a byte per value.
You can use the parse methods on the default types' class. For example, if these numbers are integers and are stored as string "34", you can use Integer.parseInt("34") which returns you a single int whose value is 34. Similarly for Double, Float and Long.
Do you want to convert the Hex number to bytes? If so, this is your answer:
Convert a string representation of a hex dump to a byte array using Java?
If you want to convert number to byte -> http://snippets.dzone.com/posts/show/93
Convert the two digit ASCII string to a byte. When you need the ASCII back just convert it to a string. The following code shows how to do this for "95".
public class Main {
public static void main(String[] args) {
byte b = Byte.parseByte("95");
String bString = "" + b;
}
}
If you need to change the way they are stored in a file, just read the two digit text numbers in as strings and write them out to another file as bytes.

Categories