I have a very big file which contains user ids like this. Each line in that big file is an user id.
149905320
1165665384
66969324
886633368
1145241312
286585320
1008665352
So in that big file, I will have around 30Million user id's. Now I am trying to select random user id's from that big file. Below is the program I have but at some point it always give me this exception like this- and I am not sure why this exception is happening.
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:59)
at java.lang.Integer.parseInt(Integer.java:481)
at java.lang.Integer.parseInt(Integer.java:510)
at com.host.bulls.service.lnp.RandomReadFromFile.main(RandomReadFromFile.java:65)
Below is the program I have-
public static void main(String[] args) throws Exception {
File f = new File("D:/abc.txt");
RandomAccessFile file;
try {
file = new RandomAccessFile(f, "r");
long file_size = file.length();
// Let's start
long chosen_byte = (long)(Math.random() * (file_size - 1));
long cur_byte = chosen_byte;
// Goto starting position
file.seek(cur_byte);
String s_LR = "";
char a_char;
// Get left hand chars
for (;;)
{
a_char = (char)file.readByte();
if (cur_byte < 0 || a_char == '\n' || a_char == '\r' || a_char == -1) break;
else
{
s_LR = a_char + s_LR;
--cur_byte;
if (cur_byte >= 0) file.seek(cur_byte);
else break;
}
}
// Get right hand chars
cur_byte = chosen_byte + 1;
file.seek(cur_byte);
for (;;)
{
a_char = (char)file.readByte();
if (cur_byte >= file_size || a_char == '\n' || a_char == '\r' || a_char == -1) break;
else
{
s_LR += a_char;
++cur_byte;
}
}
// Parse ID
if (cur_byte < file_size)
{
int chosen_id = Integer.parseInt(s_LR);
System.out.println("Chosen id : " + chosen_id);
}
else
{
throw new Exception("Ran out of bounds..");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Is there any problem in my above code?
break
you do not need toelse
– Kowatch.readByte()
when you can.readChar()
? – Kowatch