You are aloof, and that's great.
When reading a file, Reader will return null when it reaches the end of the stream, which means that nothing is readable. Your current approach means that you want to read at least 100 lines, but no more ... it will become problematic in the future if the file size increases ... it is also somewhat wasteful
Instead, we should use the fact that a null value indicates the end of the file.
When you split a line, it will contain several elements. For printing, you use the linenum variable. The problem is that you have already read and split the line, linenum not relevant for this task, because it represents the number of lines that you have already read, and not the part of the line that you just divided.
Instead, you need to use the inner loop to display separate split elements for each row ...
For instance...
BufferedReader in = null; try { in = new BufferedReader(new FileReader("fileeditor.txt")); String read = null; while ((read = in.readLine()) != null) { String[] splited = read.split("\\s+"); for (String part : splited) { System.out.println(part); } } } catch (IOException e) { System.out.println("There was a problem: " + e); e.printStackTrace(); } finally { try { in.close(); } catch (Exception e) { } }
Also, do not forget that if you open it, you must close it;)
You might want to work a little more with Basic I / O ;)
source share