while (file1.ready()) { String[] s = file1.readLine().split(" "); for (String str : s) { if (str.equals("\b[0-9]+\b")) { file2.write(str + " "); }