, BufferedReader String split():
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(inputFile));
String line;
while ((line = in.readLine()) != null) {
String[] inputLine = line.split("\\s+");
}
} catch (Exception e) {
} finally {
if (in != null) {
try {
in.close();
} catch (Exception ignored) {}
}
}
(If you are using Java 7, a block is finallynot needed if you are using try-with-resources.)
This will change something like ______45___23(where _ is a space) to an array ["45", "23"]. If you need these integers, it's pretty simple to write a function to convert the String array to an int array:
public int[] convert(String[] s) {
int[] out = new int[s.length];
for (int i=0; i < out.length; i++) {
out[i] = Integer.parseInt(s[i]);
}
return out;
}
source
share