The loop does not become infinite - just grossly inefficient. The problem with the +=
on java.lang.String
is that it produces a new immutable object, discarding the one that it held before. Each time it makes a copy, making the process an O(n2) in terms of the number of entries in your file.
The fix is straightforward - replace String s
with StringBuilder s
, and use append
in place of +=
.
StringBuilder s = new StringBuilder();
try {
File file = new File(...);
FileInputStream file_input = new FileInputStream(file);
DataInputStream data_in = new DataInputStream(file_input );
while (true) {
try {
for (int index = 0; index < 4; index++) {
byteArray[index] = data_in.readByte();
}
} catch (EOFException eof) {
break;
}
float f = readFloatLittleEndian(byteArray); // transforms 4 bytes into a float
s.append(f);
}
data_in.close();
} catch (IOException e) {
System.err.println(e.toString());
}
System.out.print(s);