How to split the strings in a file and read them?

后端 未结 3 1684
情书的邮戳
情书的邮戳 2020-12-06 17:53

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

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

I wan

相关标签:
3条回答
  • 2020-12-06 18:35

    You're part way there which is great.

    When reading a file, the Reader will return null when it reaches the end of the stream, meaning nothing else is available to be read. Your current approach means that you want to read at least 100 lines, but no more...this will become problematic in the future if you file size increases...it's also somewhat wasteful

    Instead, we should use the fact a null value indicates the end of the file..

    When you split a line, it will contain a number of elements. You are using the linenum variable to print these. The problem is, you've already read and split the line, the linenum is irrelevant for this task, as it represents the number of lines you've already read, not the part of the string you've just split.

    Instead, you need to use a inner loop to display the individual split elements for each line...

    For example...

    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, don't forget, if you open it, you musty close it ;)

    You might want to take a little more time going through Basic I/O as well ;)

    0 讨论(0)
  • 2020-12-06 18:38

    You can use a StreamTokenizer. It will split a stream into tokens according to its settings. From your question I think that you want to treat line endings just as token separators. The code would 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 woth the input you may need to set different options, the documentation should help you here.

    0 讨论(0)
  • 2020-12-06 18:54
     String[] splited = read.split("\\s+");
      for (int i= 0; i<splited.length; i++){
      System.out.println(splited[i]);
      }
    

    You should loop the result after you split the string.

    0 讨论(0)
提交回复
热议问题