?
Solved

Deleting a block of a string from a text file

Posted on 2004-10-20
29
Medium Priority
?
188 Views
Last Modified: 2010-03-31
Good morning, how are you all doing? I was wondering if someone could help me with something basically what I want to do is lets say I have this in a text file
Header|||||TEXT|||
Header1|||||.... so so on so


then another block of data
Header|||||TEXT1||
Header1||||||||.....so so on

Now what I want to do is read that file and if I find the Header with the TEXT1 in it then remove it from that file. Is that an easy thing to do in Java. Thanks

CRS
0
Comment
Question by:Corey_819
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 14
29 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12359642
Read in the file line by line.
Write the lines one by one to another file
Stop writing to the other file if you found that specific header
Start writing again if the end of that header is found
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12359674
If the file is small enough to read into memory, you can use the following class and treat it like a List:

import java.io.*;
import java.util.ArrayList;

/**
 *  A utility class the allows a text file to be
 *  treated as a collection of Strings
 *
 * @author     CEHJ
 * @created    29 February 2004
 */
public class StringList extends ArrayList {

  /**
   *Constructor for the StringList object
   */
  public StringList() {
    super();
  }

  public void read(InputStream in) {
    read(new InputStreamReader(in));
  }


  public void read(Reader r) {
    String line = null;
    BufferedReader in = null;
    try {
      in = new BufferedReader(r);
      while ((line = in.readLine()) != null) {
        add(line);
      }
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally {
      try {
        in.close();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    }

  }

  /**
   *  Constructor for the StringList object
   *
   * @param  fileName  The file to open
   */
  public void read(String fileName) {
    try {
      read(new FileReader(fileName));
    }
    catch(IOException e) {
      e.printStackTrace();
    }
  }

  /**
   *  Save the String to named file
   *
   * @param  fileName  The name of the file to save to
   */
  public void save(String fileName) {
    PrintWriter out = null;
    try {
      out = new PrintWriter(new FileOutputStream(fileName), true);
      for (int i = 0; i < size(); i++) {
        out.println((String) get(i));
      }
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally {
      if (out != null) {
        out.close();
      }
    }
  }

}
0
 
LVL 1

Author Comment

by:Corey_819
ID: 12359836
How would I know i found that specified header. If I use readline('\n\r') that will tell me when I get to the next header but not the |TEXT1| postion part of that header. put how do I read each postion in that string? Thanks for all your help guys.
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 37

Expert Comment

by:zzynx
ID: 12359889
Using String functions like indexOf()
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12359926
if ( readLine.indexOf("TEXT1")!=-1 ) {
  // the line has "TEXT1" in it
}
0
 
LVL 1

Author Comment

by:Corey_819
ID: 12363123
Exception in thread "main" java.lang.NoClassDefFoundError: Read/java

I am sorry this is probably silly question however I am still learning Java why am I getting this error above?
0
 
LVL 1

Author Comment

by:Corey_819
ID: 12363184
Disregard last question I figured out what I was doing wrong. :)
0
 
LVL 1

Author Comment

by:Corey_819
ID: 12368752
One more thing fellows. I guess I should have mentioned what exactly I was trying to do. I only want to find the line with TEXT1 in it that begins with MSH| would I just put an && statment in my if(readline.indexOf("TEXT1") !=-1 && readline.indexOf("MSH") !=-1)? Thanks for you help:)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12368813
if ( readLine.startsWith("MSH|") && readline.indexOf("TEXT1")!=-1 )
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12368828
The equivalent is

      if ( readLine.indexOf("MSH|")==0 && readline.indexOf("TEXT1")!=-1 )

In words:
The string "MSH|" is found at position 0 in the string (=the beginning)
and
The string "TEXT1" is found "somewhere" in the string
0
 
LVL 1

Author Comment

by:Corey_819
ID: 12368891
I am sorry to bother you but I am still really learning Java. Now zzynz what you said worked prefect but it only reads that MSH| line nothing under it which coud be PV1|
                                             DG1|
Then another MSH| comes up again and the code does work but it misses basically the same start of lines as above? Basically I am asking how do i get it to read that MSH| reapeatedly like it is now and look for the TEXT1 but read all the lines underneatht the MSH| and then read the next MSH| to check for the TEXT1 again. Thanks for all your help. This is really helping me learn it.
0
 
LVL 1

Author Comment

by:Corey_819
ID: 12368901
Here is the example of my while I have been working with

while(!eof){
            String line = buff.readLine();
            if ( line == null)
                  eof = true;
            
            else if(line.startsWith("MSH|") && line.indexOf("EMH") !=-1)
            
              System.out.println(line);
            }
            buff.close();
0
 
LVL 1

Author Comment

by:Corey_819
ID: 12368938
Basically I am sure I could put a whole bunc of line.startsWith("the start of the line "|) but that seems like really poor coding. There has to be a better way
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12369022
First a remark: you don't have to check for an EOF yourself. readLine() will just return null if EOF is reached.

As I said in my previous comment:
>>Stop writing to the other file if you found that specific header  
set some boolean flag
        beginFound = true;

>>Start writing again if the end of that header is found
If I understand you well, the end of a header is marked just by finding a new header (isn't it?)

         boolean beginFoud = false; // somewhere at the beginning of the read

         if ( beginFound ) {  // THE Header is already found previously
            if ( line.startsWith("MSH|" )  // another header starts here so:
              beginFound=false;            // "close" the previous header
         } else if (line.startsWith("MSH|") && line.indexOf("EMH") !=-1) ) { // THE header is found
            beginFound = true;        // Make sure all further lines are treated as belonging to THE header
            System.out.println(line);
         }

0
 
LVL 1

Author Comment

by:Corey_819
ID: 12369104
Thanks for the tip on the eof I didn't know that.

Basically it is marked by a CR(in hex) and LF(in hex) then the new header starts. If I understand you right your saying with the above code it will check if the MSH| has been found already anf if it has the bool is true and it will print the rest my headers untill it encounters another MSH| then it will check to see if the indexOf EMH is in the line? Am i following it right?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12369165
You got that right.

But, the code needs a small correction:

         if ( beginFound ) {  // THE Header is already found previously
            if ( line.startsWith("MSH|" )  // another header starts here so:
              beginFound=false;            // "close" the previous header
         }
         if ( !beginFound ) {
             if (line.startsWith("MSH|") && line.indexOf("EMH") !=-1) ) { // THE header is found
               beginFound = true;        // Make sure all further lines are treated as belonging to THE header
               System.out.println(line);
            }
         }

Explanation: if the next header is found, it also have to be checked for containing "EMH"
0
 
LVL 1

Author Comment

by:Corey_819
ID: 12369219
One the explanation the EMH will only be in MSH| now where else, well if it is I don't care I just need to check the MSH| line for the EMH positon index.
0
 
LVL 1

Author Comment

by:Corey_819
ID: 12369322
while(!eof){
            
            String line = buff.readLine();
            
            if(beginFound)
            {
                  if(line.startsWith("MSH|"))
                  
                        beginFound = false;
            }
            if(!beginFound)
            {
                  if (line.startsWith("MSH|") && line.indexOf("EMH") !=-1)
                  {
                        beginFound = true;
                        System.out.println(line);
                  }
            }
            }
Here is my while loop it is still only reading the MSH| Do i have a syntax issue.
0
 
LVL 37

Accepted Solution

by:
zzynx earned 320 total points
ID: 12369399
>> Here is my while loop it is still only reading the MSH|
You mean it is still only printing out the line containing MSH| ;°)

Of course, I forgot an extra System.out.println():

        while(!eof){
           
           String line = buff.readLine();
           
           if(beginFound)
           {
                if(line.startsWith("MSH|"))
               
                     beginFound = false;
           }
           if(!beginFound)
           {
                if (line.startsWith("MSH|") && line.indexOf("EMH") !=-1)
                {
                     beginFound = true;
                     System.out.println(line);
                }
           }
           else System.out.println(line);  // <<<<<<<<<<<<<<<<< I forgot this
     }
0
 
LVL 1

Author Comment

by:Corey_819
ID: 12369451
if(beginFound)
            {
                  if(line.startsWith("MSH|"))
                     beginFound = false;
                     System.out.println(line);
            }
            if(!beginFound)
            {
                  if (line.startsWith("MSH|") && line.indexOf("EMH") !=-1)
                  {
                        beginFound = true;
                        System.out.println(line);
                  }
            }
            }
Now if I do this I get the other lines that belong to the MSh| but the MSH| prints twice and I have an error with the if (line.startsWith("MSH|") && line.indexOf("EMH") !=-1) blank is not found. probably need to throw an exit in there. I will try that.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12369487
Please see my previous comment
0
 
LVL 1

Author Comment

by:Corey_819
ID: 12369593
Thank you sorr for the reduntant question I didn't see the comment. I was just putting the println in the wrong place. I need to check if the line is null or the  if (line.startsWith("MSH|") && line.indexOf("EMH") !=-1) doesn't meet and I need to exit the while loop where would I need to put that because right now i get a null pointer error which makes since becuase I need ot quit when I am done looking for  if (line.startsWith("MSH|") && line.indexOf("EMH") !=-1) and reading the lines under neath it.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12369665
Use

String line;
while( (line = buff.readLine())!=null )
 
    // all what is in your current while loop
}
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12369694
Well, that's a whole bunch of info for 80 points isn't it? ;°)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12369705
Thanks for accepting
0
 
LVL 1

Author Comment

by:Corey_819
ID: 12369707
Thanks man that is it. I am learning java day by day. I am use to C++ so I am just trying to get the syntax and how it works down. Thanks have a great day. :)
0
 
LVL 1

Author Comment

by:Corey_819
ID: 12369723
I know I am sorry I wish I hav more points I would give you 500 I just didn't have much. I didn't mean for it to be this much info. Thanks though:) I will hook you up if I post another question again.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12369726
>> I am learning java day by day
Success
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12369747
>> I know I am sorry I wish I hav more points I would give you 500 I just didn't have much.
No worries. Glad I could help.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question