?
Solved

Text file parsing and displayin

Posted on 2007-03-24
24
Medium Priority
?
237 Views
Last Modified: 2010-03-31
hi i have a text file as follows

<s>
a=xyz
b=yzc
c=pqr
<e>
<s>
a=123
b=456
c=789
<e>


how can i get values and display them in a file. I want to display on a webpage/file following from the file

xyz
yzc
pqr

123
456
789

0
Comment
Question by:rahulsurya
[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
  • 11
  • 10
24 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 18787222
You will need to use a BufferedReader to read the file this way:

http://www.exampledepot.com/egs/java.io/ReadLinesFromFile.html

Then you can split the string using the split () method.

String[] array = str.split ( "=" ) ;
array[1] will have "xyz" in the case of a=xyz and so on and so forth
0
 
LVL 1

Author Comment

by:rahulsurya
ID: 18787235
how can i read the values between first write them again use same variable for next set
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18787243
Come up with some code based on what I suggested and we'll help you further
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:rahulsurya
ID: 18787290
String path = "c:"+ java.io.File.separator+"Documents and Settings"+ java.io.File.separator+"ranju"+ java.io.File.separator+"rahul";
 String f = path + java.io.File.separator+"test.txt";
 try {
        BufferedReader in = new BufferedReader(new FileReader(f));
        String str;
        while ((str = in.readLine()) != null) {
          process(str);
                  
        }
        in.close();
    } catch (IOException e) {
    }
}
public static void process(String str)
{
String[] array = str.split ( "=" ) ;
}


This is what i have written, but i have some problem displaying array values as it is dynamic.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18787307
In process (), you have to take the string array and process the elements depending on what you want to do.... do you want to display them or write them to an HTML file?
0
 
LVL 1

Author Comment

by:rahulsurya
ID: 18787327
my original file has values like

url=http://...
x=..
y=...
text=....

i want to display text , pass url,x,y values to a servlet on clicking the button open url below each text.

So each jsp will display all the test corresponding to given one file and buttons to open url
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18787332
I'm missing the point - where is the button? Is it on an applet or Swing JFrame and do you want to launch the browser? Or are you already viewing a JSP on the browser? If so where is the file - client side or server side. Pls don't assume that we know your set-up without your telling us.
0
 
LVL 1

Author Comment

by:rahulsurya
ID: 18787349
My file is on server side and i want to display a jsp with texts and a submit button below each text with label " open url " .
Text will be taken from the file and url button will be redirected to servlet with values of url,x,y(again from txt file). This servlet takes this values and open the url in applet.
Hope my setup is clear :)

0
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 500 total points
ID: 18787352
Use LineNumberReader, something like this would do
 
LineNumberReader reader = new LineNumberReader(new FileReader("file"));
 
String line = reader.readLine();
while(line != null) {
    if(line.contains("=")) {
        int idx = line.indexOf("=");
        System.out.println(line.substring(line, idx+1);
    }
    line = reader.readLine();
}
0
 
LVL 1

Author Comment

by:rahulsurya
ID: 18787399
how can i make this to read one set of file at a time and store values.
(my example file has 2 sets begining with <s> and ending with <e>)
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 500 total points
ID: 18795145
With the file format being so random, you will probably have to check it yourself:

Hashtable separators = new Hashtable () ; // using a hashtable to speed up searching
separators.put ( "<s>", "<s>" ) ; // dummy value - only key is relevant
separators.put ( "<e>", "<e>" ) ;
ArrayList list = new ArrayList () ;

while ( ( str = in.readLine () ) != null )
{
  if ( separators.containsKey ( str ) )
    process ( list ) ;
 
  else
    list.add ( str ) ;

}
0
 
LVL 1

Author Comment

by:rahulsurya
ID: 18797776
i didnt understand your last code mayankeagle sir.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18797791
Pls dont call sir.

Basically you will group strings into a list until the next separator like <e> or <s> comes, and then process the bunch.
0
 
LVL 1

Author Comment

by:rahulsurya
ID: 18797795
Hi ajay your code is good but it reads say i have
text = xyz xyz xyz ...........
abc abc abc.........


it reads only first line of text as xyz xyz xyz ........... not the full para or sentence
0
 
LVL 1

Author Comment

by:rahulsurya
ID: 18797806
ok but say if first element is seperator then it has empty list to process
0
 
LVL 1

Author Comment

by:rahulsurya
ID: 18797808
and when i use split method my arraylist contains all values as
[Ljava.lang.String;@30c221, [Ljava.lang.String;@119298d, .........]
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18797809
While processing, put the logic as:

if ( list.size () > 0 )
{
  // then process
}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18797821
Then don't use split (), just process them:

for ( int i = 0, count = list.size () ; i < count ; i ++ )
{
  String value = ( String ) list.get ( i ) ;
  // process 'value'
}

Or: String[] array = ( String[] ) list.toArray ( new String[0] ) ;

for ( int i = 0 ; i < array.length ; i ++ )
{
  // process array[i]
}
0
 
LVL 1

Author Comment

by:rahulsurya
ID: 18797860
how is ArrayList passed as parameter?

I used

public static void process(ArrayList a2)
{
 ArrayList a3 = new ArrayList(a2);
.....
}

Is this correct
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18797879
Just use: public void process ( ArrayList list )

or make it static depending upon what you wanna have.
0
 
LVL 1

Author Comment

by:rahulsurya
ID: 18797887
i used this code to process

      for ( int i = 0, count = list.size () ; i < count ; i ++ )
            {
                    String value = ( String ) list.get ( i ) ;
                    if(value.contains("=")) {
            int idx = value.indexOf("=");
                    a1.add(value.substring(idx+1));}
 
 
            }

but when i use substring method for text value i get only first line not whole para
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18802719
You want to completely ignore the <s> and <e> isn' it?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

743 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