Search code examples
javabufferedreaderreadlinefileinputstreamwritetofile

How To Read A File and Look For a Specific Line to Another Line - Java


so I want to read a file and look for a specific line and the follow 50 lines or so, and remove those lines from that file and write the other lines to another file. I need some help getting started, and some ideas for an approach. I am planning on doing a simple way. For example, this will be the txt file:

hi
hey
12
14
456
234
23
bye
53
2312
434

I want to find the line hey and delete all lines from "hey" until "bye" (inclusive). and this will repeat in the file many times at various places (about 1000 times). Also, the number of lines in between will always be the same...so if possible I can even do delete from "hey" and the next ____ number of lines. Thanks for any ideas!

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

Solution

  • This should work, it takes the two words as program arguments (for example 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) {}
            }
        }
    }
    

    INPUT:

    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
    

    OUTPUT:

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