how to read from file

hi guys what i want to do is to write the stuff like this
# uefwijoo
      (
      name:Tarzan,
      age:24,
      Quali:Engineer,
      )
# uefwijoo
      (
      name:Tarzan,
      age:24,
      Quali:FirstClass
      )

so what i am doin is  


import java.util.*;
import java.io.*;

public class Transfer
{
      private DataInputStream din;
      private DataOutputStream dout;
      private StringTokenizer tokenizer;
      private Vector tokens;
      private String comment = "#";
      private String path;
      private String line = "";
      private boolean flag;
      private Hashtable ht;

      public Transfer(String path)
      {
            this.path = path;
            try
            {
                  File f = new File(path);
                  din = new DataInputStream(new FileInputStream(f));
                  dout = new DataOutputStream(new FileOutputStream("OutCome.txt"));
            }
            catch(IOException ioe)
            {
                  ioe.printStackTrace();
            }
      }

      private void transfer()
      {      
            try
            {
                  while((line = din.readLine()) != null)
                  {
                        line.trim();
                        if((line.charAt(0)) =='#')
                        {
                              flag = true;
                              String comment = line.substring(1);
                              dout.writeBytes(line);
                        }
                        else
                        {
                              flag = false;
                              if(line.indexOf("(")!= -1)
                              {
                                    line.substring(1);
                              }
                              else if(line.indexOf(")")!= -1)
                                    break;
                              System.out.println(line);
                              while(line.length()!=0)
                                    tokenizer = new StringTokenizer(line,"=");
                        //      while(tokenizer.hasMoreTokens())
                        //      {
                                    String name = tokenizer.nextToken();
                                    System.out.println(name);
                                    String value = tokenizer.nextToken();
                                    System.out.println(value);
                              
                                    
                                    for(int i = 0;i<tokens.size();i++)
                                    {
                                          dout.writeBytes(String.valueOf(tokens.elementAt(i)));
                                    }

                              //}
                        }
                        
                  }
                  System.out.println("hey man it's end of line 1");
                  
            }//try
            catch(IOException ie)
                  {
                        ie.printStackTrace();
                  }
            
      }
      public static void main(String args[])
      {
            Transfer r = new Transfer("c:"+File.separator+"ad"+File.separator+"depmasks");
            r.transfer();
      }

}
my problem is this code tokenizes the line for first time only
while loop never loops back again
i mean i should get the o/p as
name
Tarzan
age
24 so on
so wht i get is
name
Tarzan
amitdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BillyAbbottCommented:
when i compiled your code, it didn;t work anyway... it kept getting stuck in and infinte while loop at:

while(line.length()!=0)
                          tokenizer = new StringTokenizer(line,"=");


i have fixed your code so that it outputs what i think you want, and you can get it from www.cowfish.org.uk/download/Transfer.java

tell me if i've got the right idea.


0
blairdyeCommented:
try this:
import java.util.*;
import java.io.*;
public class Transfer {
      private DataInputStream din;
      private DataOutputStream dout;
      private StringTokenizer tokenizer;
      private Vector tokens;
      private String comment = "#";
      private String path;
      private String line = "";
      private boolean flag;
      private Hashtable ht;
      public Transfer(String path) {
            this.path = path;
            try {
                  File f = new File(path);
                  din = new DataInputStream(new FileInputStream(f));
                  dout = new DataOutputStream(new FileOutputStream("c:/tmp/OutCome.txt"));
                  tokens=new Vector();
                  
            } catch (IOException ioe) {
                  ioe.printStackTrace();
            }
      }
      public static void main(String args[]) {
            Transfer r = new Transfer("c:/tmp/output.txt");
            r.transfer();
      }
private void transfer() {
      int count = 0;
      try {
            while ((line = din.readLine()) != null) {
                  count++;
                  line = line.trim();
                  if ((line.charAt(0)) == '#') {
                        line = "";
                        //flag = true;
                        //String comment = line.substring(1);
                        //dout.writeBytes(line);
                  } else {
                        flag = false;
                        if (line.indexOf("(") != -1) {
                              //whats the purpose of this?
                              //line = line.substring(1);
                              //line.substring(1);
                              line = "";
                        } else
                              if (line.indexOf(")") != -1) {
                                    //whats the purpose of this?
                                    //break;
                                    line = "";
                              }
                        while (line.length() != 0) {
                              tokenizer = new StringTokenizer(line, ":");
                              // while(tokenizer.hasMoreTokens())
                              // {
                              String name = tokenizer.nextToken();
                              tokens.addElement(name);
                              String value = tokenizer.nextToken();
                              tokens.addElement(value);
                              for (int i = 0; i < tokens.size(); i++) {
                                    dout.writeBytes((String) tokens.elementAt(i));
                                    dout.writeBytes("\n");
                              }
                              tokens.setSize(0);
                              line = "";
                        }
                        
                        
                        System.out.println("hey man it's end of line " + count);
                  }
            }
      } //try
      catch (IOException ie) {
            ie.printStackTrace();
      }
}
}
Dont understand all of what you are trying to do with the commas but your output file will appear as you stipulated
Blair
0
BillyAbbottCommented:
i've done another version and put it up as www.cowfish.org.uk/download/BillTransfer.java

its a bit smaller than yours and gets rid of the bits that i don't understand, which may very well be necessary for your final implementation

you will have to change the file path before running it though...
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

rjackmanCommented:
hello friends what both of u have added is only output statement
and haven't change any thing elase
as per me our friend don't want that
he may not be able to tokanize all string that might be his problem i suppose
well amitd i am working on ur code
get result soon
0
rjackmanCommented:
hi Amitd
i think i have solve yours problem

try following


if(line.startsWith("("))
{
line1 = line.substring(1);
StringTokenizer st  = new StringTokenizer(line1,"=,");
name = st.nextToken();
value = st.nextToken();
ht = new Hashtable();
                                    ht.put(name,value);

}
then check whether "(" has reached if yes break out

else
{
line1 = line;
StringTokenizer st  = new StringTokenizer(line1,"=,");
                              while(st.hasMoreTokens())
{
name = st.nextToken();
value = st.nextToken();
ht = new Hashtable(10);//as idon't know no of tokens u have to read so specifying capacity
                                    ht.put(name,value);
}
 }
use hash table to store values so that u can retrieve it easily as compaired to vector

best luck
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
amitdAuthor Commented:
hi
 billy i tried to access the url given by u but it says program not on server
it gives me 404NotFound Error


0
amitdAuthor Commented:
hi rjack man
what u have suggested is right
it works for me
Thanx to all of u try to help me
AMIT
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.