• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to check for a string in the file and get start and end line numbers  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Team,

Could you pls help me to write the logic in the below given Java method.

Public static boolean updateNetMap(String filepath, String nodename){

// check the file pointed by filepath to have entry for nodename.
// if it is there, get the start line no and end line no
// Based on the line nos, need logic to remove the contents from the file.
}

Below is the sample node entry, which we need to identify and delete (here nodename is WAS_CD1):

WAS_CD1:\
:conn.retry.stwait=00.00.00:\
:conn.retry.stattempts=6:\
:conn.retry.ltwait=00.00.00:\
:conn.retry.ltattempts=6:\
:tcp.max.time.to.wait=0:\
:runstep.max.time.to.wait=0:\
:contact.name=:\
:contact.phone=:\
:descrip=:\
:sess.total=255:\
:sess.pnode.max=255:\
:sess.snode.max=255:\
:sess.default=1:\
:comm.info=oldadcvipa1.ipc.us.aexp.com;1364:\
:comm.transport=tcp:\
:comm.bufsize=16000:\
:pacing.send.delay=0:\
:pacing.send.count=0:


 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure!!! What part exactly do you need help with?
 
Vivek Unni
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fred,

Thanks, I need all the 3 parts mentioned in the mtd.

// logic 1 - to check the file pointed by filepath to have entry for nodename.
// logic 2 - if it is there, get the start line no and end line no
// logic 3 - Based on the line nos, need logic to remove the contents from the file.


 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so folks here aren't going to hand you the solution. we expect you to do most of the work. Broad questions get broad answers. So far, you haven't given us any clue what you need help with in a way we can help you.

Do you know how to write a program at all? Have you installed the JDK? Do you have anything written that sort of works?

You will find that if all you say is "I need to to THIS, help me", you won't get much back. But if you read the various suggestions on our How to ask questions FAQ and follow the advice listed there, you will get plenty of help.
 
Vivek Unni
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Yes, I had written it using Scanner to read the file and search for the string.

I mainly need help in two things:

1) getting the start and end line numbers once the node name is found.
2) based on the line numbers, remove the contents of the file


Sample node entry:

WAS_CD1:\ // Here's the start line number
:conn.retry.stwait=00.00.00:\
:conn.retry.stattempts=6:\
:conn.retry.ltwait=00.00.00:\
:conn.retry.ltattempts=6:\
:tcp.max.time.to.wait=0:\
:runstep.max.time.to.wait=0:\
:contact.name=:\
:contact.phone=:\
:descrip=:\
:sess.total=255:\
:sess.pnode.max=255:\
:sess.snode.max=255:\
:sess.default=1:\
:comm.info=oldadcvipa1.ipc.us.aexp.com;1364:\
:comm.transport=tcp:\
:comm.bufsize=16000:\
:pacing.send.delay=0:\
:pacing.send.count=0: // Here's the end line number
 
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you actually trying to do, if it's just remove a specific set of content from the file then you can do it without finding start and end line numbers. You read each line in and output it to the new file until you find the node marker. Then you read lines in until you find the end of the block and finally you go back to reading lines and outputting them to the new file. Finally delete the original file (or change the extension to something like .bak) and rename the new file to the original filename.
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see possible bugs emerging from your condition to check for start of node.
You do not check if a string passed to this condition is actually a start of any node.
You just check if a line contains a name of the node.

For example, if some value of :contact.name happens to contain the name of the node you will be in trouble.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!