如何拆分文件中的字符串并读取它们?

9

我有一个包含信息的文件,它看起来像这样:

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

我希望能够分割行和字符串并读取它们。我希望它看起来像这样:
    Michael
    19
    180
    Miami
    George
    ...

i used a code like this:

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

这给了我什么:

    Michael
    25
    188

我认为这可能是我的for循环出了问题,但我在编程方面并不是很高级,我会感激您的帮助。谢谢。

4个回答

10

你已经有了一部分的进展,这很棒。

在读取文件时,当Reader到达流的末尾时,它将返回null,表示没有其他可读的内容。你目前的方法意味着你想要至少读取100行,但不再需要读取更多。如果文件大小增加,这将在未来变得麻烦...这也有些浪费。

相反,我们应该利用null值表示文件结尾的事实...

当你分割一行时,它将包含许多元素。你正在使用linenum变量来打印这些元素。问题是,你已经读取和分割了这一行,linenum对于此任务是无关紧要的,因为它代表你已经读取的行数,而不是刚刚分割的字符串的部分。

相反,你需要使用一个内部循环来显示每行的单独分割元素...

例如...

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) {
    }
}

还有,别忘了,如果你打开它,你必须把它关上 ;)

你可能想花更多时间仔细阅读基本输入/输出 ;)


喜欢你的回答伙计。总是表达得很清晰明了。 - Rann Lifshitz

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

你需要在分割字符串后循环结果。

1
你可以使用StreamTokenizer。它会根据其设置将流拆分为标记。从你的问题中,我认为你想将行结尾视为标记分隔符。代码如下:
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) {
                }
            }
        }
    }
}

根据您需要对输入执行的操作,您可能需要设置不同的选项,请参阅文档以获得帮助。


重新考虑后,使用Scanner似乎更好。StreamTokenizer已经过时了。 - Viktor Seifert

0
我已经将@MadProgrammer提供的答案代码进行了重构,转换成了一个Java方法,您可以轻松地将其整合到您的开发中:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Post18607545 {
    public static void splitAndReadFile(String fileName) throws IOException {
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(fileName));
            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) {
            }
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接