【问题标题】:The output file doesn't contain all the lines i have copied from the source输出文件不包含我从源复制的所有行
【发布时间】:2014-04-22 21:26:48
【问题描述】:

下面是我写的代码,bucketsRow 包含 80 个元素,for 循环中的 println 语句正确显示了这一点。 readFile 是一个扫描器,它遍历具有 800 行的文件,其中元素是第一个单词,其中任何 80 行都存在于bucketsRow 中。我希望复制到新文件中的那些行,为此我使用了 filewriter。

for(int p=0;p<bucketsRow.length;p++)
{
    System.out.println("p:"+p+"Doc at p:"+names[p]);
    inner: while(readfile.hasNextLine())
    {
        String line=readfile.nextLine();
        //System.out.println(line);
        if(line.indexOf(names[p])!=-1)
        {
            System.out.println(line.indexOf(names[p]));
            writefile.write(line);
            writefile.write(System.getProperty( "line.separator" ));
            break inner;
        }
    }
}
writefile.flush();
writefile.close();     

【问题讨论】:

  • 这段代码到底有什么问题?

标签: java file-io java.util.scanner filewriter


【解决方案1】:

这里的问题是您需要再次将扫描仪类引用指针的焦点设置到文件的开头并再次开始比较。因此,输出文件中的某些行会丢失。

当您进入“while”循环时,为了将焦点放在文件的开头,请重新创建一个扫描仪引用。

【讨论】:

    猜你喜欢
    • 2018-02-21
    • 2012-12-23
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 2015-01-03
    • 1970-01-01
    相关资源
    最近更新 更多