user3384231
user3384231

Reputation: 4053

How to read the file when the first empty space occurs

I have a file whose content as follows:

Testing this program
This is the second line
"This is the Blank line"(Empty line)

This is first line that needs to read and stored
This is the second line that needs to read and stored

So as soon as Blank line is found, I need to start storing the data after the Blank line is detected.

I am using bufferreader and while loop to read line by line.

Any help would be appreciated.

Thanks

            FileReader inputFile = new FileReader(httpPost);
        BufferedReader bufferReader = new BufferedReader(inputFile);

        while ((line = bufferReader.readLine()) != null) 
        {
                if(line.trim().equals(""))
                {
                    //Read file after blank line is detected
                    modifieddata = modifieddata + "\n" + line;
                }
        }       

Upvotes: 0

Views: 673

Answers (1)

blazy
blazy

Reputation: 338

Am not sure i completely understand what you're trying to achieve, but you are probably trying to do this :

 while ((line = bufferReader.readLine()) != null) 
 {  
      bufferdata+=  System.getProperty("line.separator") + line; 
      if(startSaving)
      {
           modifieddata += System.getProperty("line.separator") + line;
      }
      else  
      {
          originaldata += System.getProperty("line.separator") + line;
      }

      if(line.isEmpty())
      {
           startSaving = true; 
      }
  }

  if(modifieddata.isEmpty())
  {
      modifieddata = originaldata.trim();
  }
  else
  {
      modifieddata = modifieddata.trim();
  }

  String[] array = new String[]{modifieddata, originaldata};

  //Close the buffer reader
  bufferReader.close();

  // return array;

Upvotes: 2

Related Questions