Solved

Text file parsing and displayin

Posted on 2007-03-24
24
229 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
  • 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
 
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 125 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 125 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 75
eclipse compiler vs Installed JREs option 3 73
thymeleaf natural templating vs JSP 2 65
replace a word with other 1 40
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

929 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now