How to split lines in a file and read them?

I have a file with information in it. It looks like this:

Michael 19 180 Miami George 25 176 Washington William 43 188 Seattle 

I want to break lines and lines and read them. I want it to look like this:

  Michael 19 180 Miami George ... 

I used the following code:

  BufferedReader in = null; String read; int linenum; try{ in = new BufferedReader(new FileReader("fileeditor.txt")); } catch (FileNotFoundException e) {System.out.println("There was a problem: " + e);} try{ for (linenum = 0; linenum<100; linenum++){ read = in.readLine(); if(read == null){} else{ String[] splited = read.split("\\s+"); System.out.println(splited[linenum]); } } } catch (IOException e) {System.out.println("There was a problem: " + e);} } 

What it gave me was

  Michael 25 188 

I think this is probably a problem with my for loop, but I'm not very advanced in programming, and I would appreciate help. Thanks.

+6
source share
3 answers

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 ;)

+5
source
  String[] splited = read.split("\\s+"); for (int i= 0; i<splited.length; i++){ System.out.println(splited[i]); } 

You must encode the result after splitting the string.

+2
source

You can use StreamTokenizer . It will split the stream into tokens according to its settings. From your question, I think you want to treat line endings as token separators. The code will look like this:

 import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.StreamTokenizer; public class ReaderSample { public static void main(String[] args) { BufferedReader in = null; try { in = new BufferedReader(new FileReader("fileeditor.txt")); StreamTokenizer st = new StreamTokenizer(in); st.eolIsSignificant(false); // remove comment handling st.slashSlashComments(false); st.slashStarComments(false); while(st.nextToken() != StreamTokenizer.TT_EOF) { if (st.ttype == StreamTokenizer.TT_NUMBER) { // the default is to treat numbers differently than words // also the numbers are doubles System.out.println((int)st.nval); } else { System.out.println(st.sval); } } } catch(IOException ex) { System.err.println(ex.getMessage()); } finally { if (in != null) { try { in.close(); } catch (IOException ex) { } } } } } 

Depending on what you need to do with the input, you may need to set various parameters, the documentation should help you here.

+1
source

Source: https://habr.com/ru/post/953130/


All Articles