Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

reading a section of a file  RSS feed

 
Mazi Obi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
Can you please tell me how to read a few lines within a text file?

Here is a sample of the file.
I want to search the file for the occurence of "CELL CGI"
and where this is found I want to read the next 5 lines and write lines 1,3,5 to another file as a single line.
I've learnt how to read the file and to write it as a whole, but these conditionals just stump me
Please help me with what suggestions you may have.

NAME SETNAME PARID VALUE UNIT CLASS DISTRIB
JTQ704T3 MTPC 8157 800 MS CUSTOM IMMED
STATUS FCVSET FCVALUE DCINFO FCODE
UPDATED FALSE 65423 UNDEF 0

NAME SETNAME PARID VALUE UNIT CLASS DISTRIB
FEATURE2 CME20BSCF 8156 0 FEATURE IMMED
STATUS FCVSET FCVALUE DCINFO FCODE
UPDATED FALSE 63487 UNDEF 0

CELL CGI BSIC BCCHNO AGBLK MFRMS IRC
TG03113 510-01-6196-3113 37 47 1 2 OFF

TYPE BCCHTYPE FNOFFSET XRANGE CSYSTYPE
INT NCOMB 0 NO GSM900

CELLIND
H'021

CELL CGI BSIC BCCHNO AGBLK MFRMS IRC
TG03112 510-01-6196-3112 37 35 1 2 OFF

TYPE BCCHTYPE FNOFFSET XRANGE CSYSTYPE
INT NCOMB 0 NO GSM900

CELLIND
H'020
 
u johansson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you read the file and then you write something to the target file.

What's the problem?
 
Mazi Obi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This is the problem:
In the sample file content I pasted, I want to read only the section that begins with "CELL CGI BSIC BCCHNO AGBLK MFRMS IRC".
This line "CELL CGI BSIC BCCHNO AGBLK MFRMS IRC" and the next five lines are the same record.

The lines:
"CELL CGI BSIC BCCHNO AGBLK MFRMS IRC"
"TYPE BCCHTYPE FNOFFSET XRANGE CSYSTYPE"
"CELLIND"
are column headings and I do not want to copy them.

The other lines:
"TG03112 510-01-6196-3112 37 35 1 2 OFF"
"INT NCOMB 0 NO GSM900"
"H'020 "

are the ones I want to write to a new file as a single line.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's one approach:

  • Read a line from the input file, and test to see if it starts with "CELL CGI."
  • If not, keep repeating the above process. But if so...
  • Read the next line and append it to a StringBuffer.
  • Read the next line and do nothing with it.
  • Read the next line and append it to the StringBuffer.
  • Read the next line and do nothing with it.
  • Read the next line and append it to the StringBuffer.
  • Convert the StringBuffer to a String, and write it to the output file.
  •  
    Mazi Obi
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Marc,
    Will the simple + operator be sufficient to append a new line to the buffer?
    I used the readLine class, but do not know how to tell it what line to read or what line to skip.
    can you advice me on how to do this logic?
    Thanks once again.
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Be sure to check the API documentation.

    In addition to addition, the + operator is overloaded to concatenate Strings. This has fairly high overhead, which is one reason that a StringBuffer (or StringBuilder) is preferable. But a StringBuffer is not a String -- it is an object with it's own methods (defined in the API). One of these methods is append(str)...

    The readLine() method (in the class BufferedReader) reads a line of text and returns a String containing the contents of the line. In this situation, you can test each line using String's startsWith(str) method. Each time you call the readLine method, it will advance to the next line, so you can do something like...

    (Be sure to check for null values along the way. )
    [ December 28, 2005: Message edited by: marc weber ]
     
    Mazi Obi
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Marc,
    The methods you suggested worked quite well.
    I guess this Java thing is getting interesting finally.
    I hope to be as good as you guys some day.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!