Solved

Text file parsing and displayin

Posted on 2007-03-24
24
228 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:mayankeagle
Comment Utility
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
Comment Utility
how can i read the values between first write them again use same variable for next set
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Come up with some code based on what I suggested and we'll help you further
0
 
LVL 1

Author Comment

by:rahulsurya
Comment Utility
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:mayankeagle
Comment Utility
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
Comment Utility
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:mayankeagle
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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:mayankeagle
mayankeagle earned 125 total points
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 1

Author Comment

by:rahulsurya
Comment Utility
i didnt understand your last code mayankeagle sir.
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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
Comment Utility
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
Comment Utility
ok but say if first element is seperator then it has empty list to process
0
 
LVL 1

Author Comment

by:rahulsurya
Comment Utility
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:mayankeagle
Comment Utility
While processing, put the logic as:

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

Expert Comment

by:mayankeagle
Comment Utility
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
Comment Utility
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:mayankeagle
Comment Utility
Just use: public void process ( ArrayList list )

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

Author Comment

by:rahulsurya
Comment Utility
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:mayankeagle
Comment Utility
You want to completely ignore the <s> and <e> isn' it?
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 …
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.

771 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