【问题标题】:How To Read A File and Look For a Specific Line to Another Line - Java如何读取文件并查找特定行到另一行 - Java
【发布时间】:2014-07-31 14:03:49
【问题描述】:

所以我想读取一个文件并查找一个特定的行,然后是 50 行左右,然后从该文件中删除这些行并将其他行写入另一个文件。我需要一些帮助开始,以及一些方法的想法。我打算做一个简单的方法。例如,这将是 txt 文件:

hi
hey
12
14
456
234
23
bye
53
2312
434

我想找到行 hey 并删除从“hey”到“bye”(包括)的所有行。这将在文件中在不同的地方重复多次(大约 1000 次)。此外,两者之间的行数将始终相同......所以如果可能的话,我什至可以从“嘿”和下一个____行中删除。感谢您的任何想法!

FileInputStream fstream = new FileInputStream("file name");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

String strLine; 

//Read File Line By Line
while ((strLine = br.readLine()) != null)   {
  // Print the content on the console
  System.out.println (strLine);
}

//Close the input stream
br.close();
}

【问题讨论】:

  • 您使用的是什么操作系统?如果您使用的是 Linux 或 OS X,那么您也许可以使用 sed 命令来执行此操作。
  • 等一下,所以你只需要逐行读取文件,如果你遇到开始的那一行就开始忽略这些行,然后当你遇到结尾的那一行然后忽略它并开始不忽略线?我理解正确吗?因为这很简单,只要确保我理解正确即可
  • 我没有使用 Linux 或 OS X,但是如果我找到起跑线,我就必须寻找它停止的终点。但这对起止行会在文件中出现多次,而不是一次。

标签: java bufferedreader readline fileinputstream writetofile


【解决方案1】:

这应该可以,它将两个词作为程序参数(例如MyApp.jar hey bye

public class Main
{
     public static void main(String[] args)
     {
         if(args.length < 2) 
         {
             System.exit(0);
         }
         String ignoreStartWord = args[0];
         String ignoreEndWord = args[1];
         BufferedReader br = null;
         BufferedWriter bw = null;
         try
         {
             br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("fileName"))));
             bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("fileName2"))));
             boolean ignoreStarted = false;
             String line = null;
             while((line = br.readLine()) != null)
             {
                 if(ignoreStarted == false)
                 {
                     if(line.equals(ignoreStartWord))
                     {
                         ignoreStarted = true;
                     }
                     else
                     {
                         bw.write(line);
                         bw.newLine();
                     }
                 }
                 else
                 {
                     if(line.equals(ignoreEndWord))
                     {
                         ignoreStarted = false;
                     }
                 }
            }
        }
        catch(IOException e)
        {
             e.printStackTrace();
        }
        catch(FileNotFoundException e)
        {
             e.printStackTrace();
        }
        finally
        {
             if(br != null) try { br.close(); } catch(IOException e) {}
             if(bw != null) try { bw.close(); } catch(IOException e) {}
        }
    }
}

输入:

hi
hey
12
14
456
234
23
bye
53
2312
434
hi
hey
12
14
456
234
23
bye
53
2312
434
hi
hey
12
14
456
234
23
bye
53
2312
434
hi
hey
12
14
456
234
23
bye
53
2312
434

输出:

hi
53
2312
434
hi
53
2312
434
hi
53
2312
434
hi
53
2312
434

【讨论】:

  • 这是我期待的输出!虽然你如何做程序参数?另外,如果该行以另一个单词开头怎么办?说像“1244 错误嘿 235 fk5rft 等...如果忽略词中有空格,它将不会读取它...谢谢
  • errrr 目前它只检查整行是否与您提供的单词匹配,并且该单词在规范中没有空格。程序参数,例如,如果您从 Eclipse 运行代码,则可以选择指定它,尽管在 Eclipse 中我倾向于将文件导出为可运行的 JAR 文件,然后使用'java -jar export.jar命令行中的argument1 argument2'
  • 我很确定如果程序参数让你感到困惑,那么你可以从文件中加载它们,如果你想从侧面删除空格,那么你可以调用 trim()。跨度>
  • 谢谢,我真的把它修好了。我能够成功运行它。现在我只是想让它做一对以上的关键字
  • 这应该不难,一旦你弄清楚如何为 2 做,那么你就可以为 n 做。基本算法是相同的,真正的魔力是同时处理多个“忽略”状态,也就是说,您需要存储哪个词引发了忽略,然后只有在遇到其密钥对关闭词时才结束它。跨度>
猜你喜欢
  • 2017-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多