如何拆分文件中的字符串并读取它们? [英] How to split the strings in a file and read them?

查看:78
本文介绍了如何拆分文件中的字符串并读取它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含信息的文件.看起来像:

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

我想分割线和字符串并阅读它们.我希望它看起来像:

    Michael
    19
    180
    Miami
    George
    ...

我使用了这样的代码:

    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循环有关,但我的编程水平不是很高,我将不胜感激.谢谢.

解决方案

您参与其中,真是太好了.

读取文件时,Reader在到达流的末尾时将返回null,这意味着没有其他内容可供读取.您当前的方法意味着您希望读取至少100行,但是不要再读取了...如果文件大小增加,将来这将成为问题...这也有些浪费

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

分割一行时,它将包含许多元素.您正在使用linenum变量来打印这些.问题是,您已经读取并分割了行,linenum与该任务无关,因为它表示您已经读取的行数,而不是您刚刚分割的字符串的一部分./p>

相反,您需要使用内部循环来显示每行的单独拆分元素...

例如...

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

另外,别忘了,如果您打开它,就必须将其关闭;)

您可能需要花更多时间进行基本I/O 以及;)

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

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

I want to split the lines and strings and read them. I want it to look like:

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

What this gave me was

    Michael
    25
    188

I think its probably an issue with my for loop but I'm not very advanced in programming and I'll appreciate help. Thanks.

解决方案

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

这篇关于如何拆分文件中的字符串并读取它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆